Skip to content

[DAL] Add MRT Data Store Context and Hooks for Template Usage - #3834

Merged
adamraya merged 15 commits into
developfrom
bendvc/W-22385616_dal-hooks
May 22, 2026
Merged

[DAL] Add MRT Data Store Context and Hooks for Template Usage #3834
adamraya merged 15 commits into
developfrom
bendvc/W-22385616_dal-hooks

Conversation

@bendvc

@bendvc bendvc commented May 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds React Context provider and hooks to make MRT Data Store custom preferences easily accessible in template applications. The provider is automatically integrated into
the SSR rendering pipeline, requiring no manual setup from developers.

Changes

New Features (pwa-kit-react-sdk)

Context & Provider

  • Added MrtDataStoreContext and MrtDataStoreProvider to src/ssr/universal/contexts/index.js
  • Provider automatically switches data source:
    • Server (SSR): Receives preferences as props from SSR bootstrap
    • Client: Reads from window.MRT_DATA_STORE (serialized by server)
  • Integrated into both server (react-rendering.js) and client (main.jsx) rendering pipelines

Hooks

  • Added useCustomSitePreferences() - Returns site-specific preferences
  • Added useCustomGlobalPreferences() - Returns global preferences
  • Both hooks exported from @salesforce/pwa-kit-react-sdk/ssr/universal/hooks

Tests

  • Added 5 comprehensive tests for MrtDataStoreProvider:
    • Provides empty objects when no data available
    • Uses SSR props on server
    • Reads from window on client
    • Prefers window over SSR props (client hydration behavior)
    • Handles missing nested properties gracefully
  • All tests passing, 96.29% coverage

Usage Example

                                                                                                                                                                            
  import {                                                                                                                                                                  
      useCustomSitePreferences,                                                                                                                                             
      useCustomGlobalPreferences                                                                                                                                            
  } from '@salesforce/pwa-kit-react-sdk/ssr/universal/hooks'                                                                                                                
                                                                                                                                                                            
  const MyComponent = () => {                                                                                                                                               
      const sitePrefs = useCustomSitePreferences()                                                                                                                          
      const globalPrefs = useCustomGlobalPreferences()                                                                                                                      
                                                                                                                                                                            
      return <div>{sitePrefs.myFeatureFlag ? 'Enabled' : 'Disabled'}</div>                                                                                                  
  }      

No setup required - provider is automatically integrated into the rendering pipeline!

Testing the Demo

  1. Enable MRT Data Store in config/default.js:
    app: {
    mrtDataStore: {
    enabled: true
    }
    }

  2. Run with local data:
    MRT_DATA_STORE_DEFAULTS='{"custom-global-preferences":{"theme":"dark"},"RefArch-custom-site-preferences":{"maxItems":10}}' PWAKIT_MRT_DATA_STORE_ENABLED=true npm start

  3. Visit http://localhost:3000/demo-mrt-data-store

Files Changed

pwa-kit-react-sdk:

  • src/ssr/universal/contexts/index.js - Added provider & context
  • src/ssr/universal/contexts/index.test.js - Added tests
  • src/ssr/universal/hooks/index.js - Added hooks
  • src/ssr/server/react-rendering.js - Integrated provider into SSR
  • src/ssr/browser/main.jsx - Integrated provider into client
  • CHANGELOG.md

template-retail-react-app:

  • app/pages/demo-mrt-data-store/index.jsx - New demo page
  • app/routes.jsx - Added route
  • CHANGELOG.md

Separate Bug Fix (for another branch)

Bug: Signature Mismatch Between Client and Server Functions

Problem: Client and server versions of getCustomSitePreferences and getCustomGlobalPreferences have incompatible signatures:

  • Server: async getCustomSitePreferences({siteId}) (async, requires params)
  • Client: getCustomSitePreferences() (sync, no params)

Solution: Make both versions async and accept same parameters for consistency.

Files to change (pwa-kit-runtime):

  • src/utils/data-store/ssr-site-preferences.client.js - Make async, add params
  • src/utils/data-store/ssr-global-preferences.client.js - Make async
  • src/utils/data-store/ssr-site-preferences.test.js - Update tests to use await
  • src/utils/data-store/ssr-global-preferences.test.js - Update tests to use await
  • CHANGELOG.md - Add bug fix entry

@bendvc
bendvc requested a review from a team as a code owner May 15, 2026 17:29
@git2gus

git2gus Bot commented May 15, 2026

Copy link
Copy Markdown

Git2Gus App is installed but the .git2gus/config.json doesn't have right values. You should add the required configuration.

@cc-prodsec

cc-prodsec commented May 15, 2026

Copy link
Copy Markdown
Collaborator

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@bendvc
bendvc requested a review from adamraya May 15, 2026 21:03

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to keep the .md file if so should we move it to the template-retail-react-app/docs/ folder?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry.. this file wasn't supposed to be included, it was something I was working on for someone else.

Comment on lines +19 to +22
const MrtDataStoreContext = createContext({
customSitePreferences: {},
customGlobalPreferences: {}
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should we keep the exiting pattern of using React.createContext() or updating to consolidate one pattern?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Updated.

Comment on lines +94 to +95
customSitePreferences: window.__MRT_DATA_STORE__.customSitePreferences || {},
customGlobalPreferences: window.__MRT_DATA_STORE__.customGlobalPreferences || {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should we use the same constants we use in the react-rendering?

windowGlobals[DATA_STORE_WINDOW_GLOBAL] = {
[DATA_STORE_BOOTSTRAP_SITE_PREFERENCES_KEY]: customSitePreferences ?? {},
[DATA_STORE_BOOTSTRAP_GLOBAL_PREFERENCES_KEY]: customGlobalPreferences ?? {}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another good catch. Fixed.

windowGlobals[DATA_STORE_WINDOW_GLOBAL] = {
[DATA_STORE_BOOTSTRAP_SITE_PREFERENCES_KEY]: customSitePreferences ?? {},
[DATA_STORE_BOOTSTRAP_GLOBAL_PREFERENCES_KEY]: customGlobalPreferences ?? {}
__siteId: siteId, // Include siteId so client can construct keys

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should use a constant like DATA_STORE_BOOTSTRAP_SITE_ID_KEY = '__siteId' form constants.js instead of the '__siteId' string directly file and re-use it in these two places?:

__siteId: siteId, // Include siteId so client can construct keys

? window.__MRT_DATA_STORE__.__siteId

@adamraya

Copy link
Copy Markdown
Contributor

It looks we no longer need DATA_STORE_BOOTSTRAP_SITE_PREFERENCES_KEY and DATA_STORE_BOOTSTRAP_GLOBAL_PREFERENCES_KEY:

/**
* Property under `window[DATA_STORE_WINDOW_GLOBAL]` for resolved site-scoped preferences.
* (DAL key is still `<siteId>-custom-site-preferences`; this is only the bootstrap object shape.)
*/
export const DATA_STORE_BOOTSTRAP_SITE_PREFERENCES_KEY = 'customSitePreferences'
/** Property under `window[DATA_STORE_WINDOW_GLOBAL]` for resolved global preferences. */
export const DATA_STORE_BOOTSTRAP_GLOBAL_PREFERENCES_KEY = 'customGlobalPreferences'

expect(data[DATA_STORE_WINDOW_GLOBAL]).toEqual({
[DATA_STORE_BOOTSTRAP_SITE_PREFERENCES_KEY]: {},
[DATA_STORE_BOOTSTRAP_GLOBAL_PREFERENCES_KEY]: {}
__siteId: undefined,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment from @adamraya on this line as well

Replace `__siteId` literal with new `DATA_STORE_BOOTSTRAP_SITE_ID_KEY` constant
across the bootstrap writer, client reader, and the rendering test. Remove the
now-unused `DATA_STORE_BOOTSTRAP_SITE_PREFERENCES_KEY` /
`DATA_STORE_BOOTSTRAP_GLOBAL_PREFERENCES_KEY` exports and their imports in the
data-store test files.
vcua-mobify
vcua-mobify previously approved these changes May 20, 2026

@vcua-mobify vcua-mobify left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

adamraya
adamraya previously approved these changes May 21, 2026
`compile-folder.js` and `compile-pseudo.js` were spawning `formatjs` via
`exec` and returning before the callback fired. In the generated test
project, npm postinstall finishes while formatjs is still writing
`app/static/translations/compiled/en-XA.json`, leaving the file empty
when tests run and breaking `locale.test.js > loading the pseudo locale`
on Node 24 CI runners. Switch to `execSync` so the postinstall blocks
until formatjs has finished.
adamraya added 2 commits May 21, 2026 13:41
… and changelog entry

Reverts:
- bf3aed5 Add CHANGELOG entry for translation compile script fix
- 0636c23 Make translation compile scripts wait for formatjs to finish

The locale.test.js failure on Node 24 CI is caused by `@formatjs/cli@>=6.15.0`
shipping native binaries that silently emit
"Warning: Pseudo-locale transformations not yet implemented" and produce a
single-element array instead of the expected 3-element pseudo-localized output.
That root cause is fixed in #3842 (pin `@formatjs/cli` to 6.9.0 + verdaccio
config to stop proxying monorepo packages). Reverting both commits since they
were addressing the wrong symptom; we'll merge develop in once #3842 lands.
vcua-mobify
vcua-mobify previously approved these changes May 22, 2026
…dal-hooks

# Conflicts:
#	packages/pwa-kit-runtime/CHANGELOG.md
@adamraya
adamraya enabled auto-merge May 22, 2026 22:25
adamraya
adamraya previously approved these changes May 22, 2026
vcua-mobify
vcua-mobify previously approved these changes May 22, 2026
PR's vendor.js is 395.33 KB gzip (was 394.78 KB on develop) — the new
MrtDataStoreProvider context, useCustomSitePreferences /
useCustomGlobalPreferences hooks, and the bootstrap-key wiring add ~0.55 KB
to the shipped client bundle. Raise the budget by 2 kB so compile variance
doesn't flake CI without giving up the guardrail.
@adamraya
adamraya dismissed stale reviews from vcua-mobify and themself via 2299ae5 May 22, 2026 22:32
@adamraya
adamraya merged commit 2bb709f into develop May 22, 2026
76 of 78 checks passed
@adamraya
adamraya deleted the bendvc/W-22385616_dal-hooks branch May 22, 2026 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants