Skip to content

Commit

Permalink
fix: fast ssr access to globals should be allowed ouside of a request…
Browse files Browse the repository at this point in the history
… after adding RequestStorageManager middleware (microsoft#6318)

* Updated installDOMShim logic so original global values are returned from getter if not in request scope

* Change files

* After install DOM Shim, globals will now return the original global instead of the shimmed version in async local storage if code is not in storage scope.

* Update change/@microsoft-fast-ssr-e107db57-9534-4ac4-bd65-157d5b40a79f.json

Co-authored-by: Rob Eisenberg <EisenbergEffect@users.noreply.github.com>

Co-authored-by: Rob Eisenberg <EisenbergEffect@users.noreply.github.com>
  • Loading branch information
erhuan-msft and EisenbergEffect authored Aug 26, 2022
1 parent 6fae6f0 commit 4fe1bc5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "After install DOM Shim, globals will now return the original global instead of the shimmed version in async local storage if code is not in storage scope.",
"packageName": "@microsoft/fast-ssr",
"email": "erhuan@microsoft.com",
"dependentChangeType": "prerelease"
}
23 changes: 23 additions & 0 deletions packages/web-components/fast-ssr/src/request-storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ test.describe("RequestStorageManager", () => {
expect(captured).toBe("world");
expect(() => RequestStorage.get("hello")).toThrow(noStorageError);
});

test("can get value from global without being in a storage scope", () => {
// window is part of perRequestGlobals setup by installDOMShim
(window as any)["hello"] = "world";
RequestStorageManager.installDOMShim();

expect((window as any)["hello"]).toBe("world");
});

test("can get different value from global in a storage scope", () => {

// window is part of perRequestGlobals setup by installDOMShim
(window as any)["hello"] = "world";
RequestStorageManager.installDOMShim();

let captured;
const storage = RequestStorageManager.createStorage();
RequestStorageManager.run(storage, () => {
captured = (window as any)["hello"];
});

expect(captured).not.toBe("world");
});
});

test.describe("RequestStorage", () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/web-components/fast-ssr/src/request-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ export const RequestStorageManager = Object.freeze({
*/
installDOMShim(): void {
for (const key of perRequestGlobals) {
const original = (globalThis as any)[key];
Reflect.defineProperty(globalThis, key, {
get() {
return RequestStorage.get("window")[key];
// Return original global variable if currently not in the storage scope
const store = asyncLocalStorage.getStore() as Map<string, any>;
return store ? store.get("window")[key] : original;
},
});
}
Expand Down

0 comments on commit 4fe1bc5

Please sign in to comment.