-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
944f7e7
commit e11f952
Showing
4 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
'@cloudflare/next-on-pages': patch | ||
--- | ||
|
||
fix: implement route specific global scoping strategy | ||
|
||
currently routes all share the same global scope, this can be problematic and cause | ||
race conditions and failures | ||
|
||
One example of this is the following code that is present in route function files: | ||
|
||
```ts | ||
self.webpackChunk_N_E = ... | ||
``` | ||
|
||
and | ||
|
||
```ts | ||
self.webpackChunk_N_E.push(...) | ||
``` | ||
|
||
this indicates that an in-memory global collection of the webpack chunks is shared by all routes, | ||
this combined with the fact that chunks can have their own module state this can easily cause routes | ||
to conflict with each other at runtime. | ||
|
||
So, in order to solve the above issue wrap every route function in a function wrapper which | ||
accepts as parameters, thus overrides, the `self`, `globalThis` and `global` symbols. The symbols | ||
are then to be resolved with proxies that redirect setters to route-scoped in-memory maps and | ||
getters to the above mentioned map's values and fallback to the original symbol values otherwise | ||
(i.e. `globalThis` will be overridden by a proxy that, when setting values, sets them in a separate | ||
location and, when getting values, gets them from said location if present there or from the real | ||
`globalThis` otherwise) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/next-on-pages/templates/_worker.js/routesIsolation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* The next-on-pages worker needs to isolate the global scope for each route, they all sharing the same global scope | ||
* allows for race conditions and incorrect behaviors (see: https://github.com/cloudflare/next-on-pages/issues/805) | ||
* | ||
* So we set up an isolation system in which each route can access a proxy to the global scope that is route-specific, | ||
* they can do that by calling `globalThis.getProxyFor(route)`. | ||
* | ||
* The following function sets up such utility alongside a map that is used to store the various proxies. | ||
*/ | ||
export function setupRoutesIsolation() { | ||
globalThis.__nextOnPagesRoutesIsolation ??= { | ||
_map: new Map(), | ||
getProxyFor, | ||
}; | ||
} | ||
|
||
/** | ||
* Utility to retrieve a route-specific proxy to the global scope, if the proxy doesn't yet exist it gets created | ||
* by the function. | ||
* | ||
* @param route the target route | ||
* @returns the proxy for the route | ||
*/ | ||
function getProxyFor(route: string) { | ||
const existingProxy = globalThis.__nextOnPagesRoutesIsolation._map.get(route); | ||
if (existingProxy) { | ||
return existingProxy; | ||
} | ||
|
||
const newProxy = createNewRouteProxy(); | ||
globalThis.__nextOnPagesRoutesIsolation._map.set(route, newProxy); | ||
return newProxy; | ||
} | ||
|
||
/** | ||
* Creates a new route-specific proxy to the global scope. | ||
* | ||
* How the proxy works: setters on the proxy don't set the values to the actual global scope but | ||
* in an internal map specific to the proxy. getters retrieve values from such internal map, and | ||
* fall back to the actual global scope for values not present in such map. | ||
* | ||
* This makes it so that routes trying to modify the global scope will, though this proxy, work | ||
* exactly like if they were actually updating the global scope, but without actually doing so, | ||
* thus not effecting any other route. | ||
* | ||
* Note: this does not account for routes trying to update already existing objects in the global | ||
* scope (e.g. `globalScope.existing_field.x = 123`), fortunately such granular control doesn't | ||
* seem necessary in next-on-pages. | ||
* | ||
* @returns the created proxy. | ||
*/ | ||
function createNewRouteProxy() { | ||
const overrides = new Map<string | symbol, unknown>(); | ||
|
||
return new Proxy(globalThis, { | ||
get: (_, property) => { | ||
if (overrides.has(property)) { | ||
return overrides.get(property); | ||
} | ||
return Reflect.get(globalThis, property); | ||
}, | ||
set: (_, property, value) => { | ||
overrides.set(property, value); | ||
return true; | ||
}, | ||
}); | ||
} | ||
|
||
type RoutesIsolation = { | ||
_map: Map<string, unknown>; | ||
getProxyFor: (route: string) => unknown; | ||
}; | ||
|
||
declare global { | ||
// eslint-disable-next-line no-var | ||
var __nextOnPagesRoutesIsolation: RoutesIsolation; | ||
} |