@tanstack/router-core 1.168.15 → 1.168.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +3 -0
- package/dist/cjs/index.d.cts +1 -1
- package/dist/cjs/manifest.cjs +31 -0
- package/dist/cjs/manifest.cjs.map +1 -1
- package/dist/cjs/manifest.d.cts +8 -0
- package/dist/cjs/ssr/ssr-server.cjs +71 -5
- package/dist/cjs/ssr/ssr-server.cjs.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/manifest.d.ts +8 -0
- package/dist/esm/manifest.js +28 -1
- package/dist/esm/manifest.js.map +1 -1
- package/dist/esm/ssr/ssr-server.js +71 -5
- package/dist/esm/ssr/ssr-server.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +7 -1
- package/src/manifest.ts +46 -0
- package/src/ssr/ssr-server.ts +125 -6
package/dist/cjs/index.cjs
CHANGED
|
@@ -35,6 +35,7 @@ exports.buildDevStylesUrl = require_utils.buildDevStylesUrl;
|
|
|
35
35
|
exports.cleanPath = require_path.cleanPath;
|
|
36
36
|
exports.composeRewrites = require_rewrite.composeRewrites;
|
|
37
37
|
exports.createControlledPromise = require_utils.createControlledPromise;
|
|
38
|
+
exports.createInlineCssStyleAsset = require_manifest.createInlineCssStyleAsset;
|
|
38
39
|
exports.createNonReactiveMutableStore = require_stores.createNonReactiveMutableStore;
|
|
39
40
|
exports.createNonReactiveReadonlyStore = require_stores.createNonReactiveReadonlyStore;
|
|
40
41
|
exports.createRawStreamDeserializePlugin = require_RawStream.createRawStreamDeserializePlugin;
|
|
@@ -59,10 +60,12 @@ exports.getElementScrollRestorationEntry = require_scroll_restoration.getElement
|
|
|
59
60
|
exports.getInitialRouterState = require_router.getInitialRouterState;
|
|
60
61
|
exports.getLocationChangeInfo = require_router.getLocationChangeInfo;
|
|
61
62
|
exports.getMatchedRoutes = require_router.getMatchedRoutes;
|
|
63
|
+
exports.getStylesheetHref = require_manifest.getStylesheetHref;
|
|
62
64
|
exports.handleHashScroll = require_hash_scroll.handleHashScroll;
|
|
63
65
|
exports.interpolatePath = require_path.interpolatePath;
|
|
64
66
|
exports.invariant = require_invariant.invariant;
|
|
65
67
|
exports.isDangerousProtocol = require_utils.isDangerousProtocol;
|
|
68
|
+
exports.isInlinableStylesheet = require_manifest.isInlinableStylesheet;
|
|
66
69
|
exports.isMatch = require_Matches.isMatch;
|
|
67
70
|
exports.isModuleNotFoundError = require_utils.isModuleNotFoundError;
|
|
68
71
|
exports.isNotFound = require_not_found.isNotFound;
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -8,7 +8,7 @@ export type { RouteToPath, TrailingSlashOptionByRouter, ParseRoute, CodeRouteToP
|
|
|
8
8
|
export type { InferFileRouteTypes, FileRouteTypes, FileRoutesByPath, CreateFileRoute, LazyRoute, LazyRouteOptions, CreateLazyFileRoute, } from './fileRoute.cjs';
|
|
9
9
|
export type { ParsedLocation } from './location.cjs';
|
|
10
10
|
export type { Manifest, RouterManagedTag, AssetCrossOrigin, AssetCrossOriginConfig, ManifestAssetLink, } from './manifest.cjs';
|
|
11
|
-
export { getAssetCrossOrigin, resolveManifestAssetLink } from './manifest.cjs';
|
|
11
|
+
export { createInlineCssStyleAsset, getAssetCrossOrigin, getStylesheetHref, isInlinableStylesheet, resolveManifestAssetLink, } from './manifest.cjs';
|
|
12
12
|
export { isMatch } from './Matches.cjs';
|
|
13
13
|
export type { AnyMatchAndValue, FindValueByIndex, FindValueByKey, CreateMatchAndValue, NextMatchAndValue, IsMatchKeyOf, IsMatchPath, IsMatchResult, IsMatchParse, IsMatch, RouteMatch, RouteMatchExtensions, MakeRouteMatchUnion, MakeRouteMatch, AnyRouteMatch, MakeRouteMatchFromRoute, MatchRouteOptions, } from './Matches.cjs';
|
|
14
14
|
export { joinPaths, cleanPath, trimPathLeft, trimPathRight, trimPath, removeTrailingSlash, exactPathTest, resolvePath, interpolatePath, } from './path.cjs';
|
package/dist/cjs/manifest.cjs
CHANGED
|
@@ -11,8 +11,39 @@ function resolveManifestAssetLink(link) {
|
|
|
11
11
|
};
|
|
12
12
|
return link;
|
|
13
13
|
}
|
|
14
|
+
function getStylesheetHref(asset) {
|
|
15
|
+
if (asset.tag !== "link") return void 0;
|
|
16
|
+
const rel = asset.attrs?.rel;
|
|
17
|
+
const href = asset.attrs?.href;
|
|
18
|
+
if (typeof href !== "string") return void 0;
|
|
19
|
+
if (!(typeof rel === "string" ? rel.split(/\s+/) : []).includes("stylesheet")) return void 0;
|
|
20
|
+
return href;
|
|
21
|
+
}
|
|
22
|
+
function isInlinableStylesheet(manifest, asset) {
|
|
23
|
+
const href = getStylesheetHref(asset);
|
|
24
|
+
return !!href && manifest?.inlineCss?.styles[href] !== void 0;
|
|
25
|
+
}
|
|
26
|
+
function createInlineCssStyleAsset(css) {
|
|
27
|
+
return {
|
|
28
|
+
tag: "style",
|
|
29
|
+
attrs: { suppressHydrationWarning: true },
|
|
30
|
+
inlineCss: true,
|
|
31
|
+
children: css
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function createInlineCssPlaceholderAsset() {
|
|
35
|
+
return {
|
|
36
|
+
tag: "style",
|
|
37
|
+
attrs: { suppressHydrationWarning: true },
|
|
38
|
+
inlineCss: true
|
|
39
|
+
};
|
|
40
|
+
}
|
|
14
41
|
//#endregion
|
|
42
|
+
exports.createInlineCssPlaceholderAsset = createInlineCssPlaceholderAsset;
|
|
43
|
+
exports.createInlineCssStyleAsset = createInlineCssStyleAsset;
|
|
15
44
|
exports.getAssetCrossOrigin = getAssetCrossOrigin;
|
|
45
|
+
exports.getStylesheetHref = getStylesheetHref;
|
|
46
|
+
exports.isInlinableStylesheet = isInlinableStylesheet;
|
|
16
47
|
exports.resolveManifestAssetLink = resolveManifestAssetLink;
|
|
17
48
|
|
|
18
49
|
//# sourceMappingURL=manifest.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.cjs","names":[],"sources":["../../src/manifest.ts"],"sourcesContent":["export type AssetCrossOrigin = 'anonymous' | 'use-credentials'\n\nexport type AssetCrossOriginConfig =\n | AssetCrossOrigin\n | Partial<Record<'modulepreload' | 'stylesheet', AssetCrossOrigin>>\n\nexport type ManifestAssetLink =\n | string\n | {\n href: string\n crossOrigin?: AssetCrossOrigin\n }\n\nexport function getAssetCrossOrigin(\n assetCrossOrigin: AssetCrossOriginConfig | undefined,\n kind: 'modulepreload' | 'stylesheet',\n): AssetCrossOrigin | undefined {\n if (!assetCrossOrigin) {\n return undefined\n }\n\n if (typeof assetCrossOrigin === 'string') {\n return assetCrossOrigin\n }\n\n return assetCrossOrigin[kind]\n}\n\nexport function resolveManifestAssetLink(link: ManifestAssetLink) {\n if (typeof link === 'string') {\n return { href: link, crossOrigin: undefined }\n }\n\n return link\n}\n\nexport type Manifest = {\n routes: Record<\n string,\n {\n filePath?: string\n preloads?: Array<ManifestAssetLink>\n assets?: Array<RouterManagedTag>\n }\n >\n}\n\nexport type RouterManagedTag =\n | {\n tag: 'title'\n attrs?: Record<string, any>\n children: string\n }\n | {\n tag: 'meta' | 'link'\n attrs?: Record<string, any>\n children?: never\n }\n | {\n tag: 'script'\n attrs?: Record<string, any>\n children?: string\n }\n | {\n tag: 'style'\n attrs?: Record<string, any>\n children?: string\n }\n"],"mappings":";AAaA,SAAgB,oBACd,kBACA,MAC8B;AAC9B,KAAI,CAAC,iBACH;AAGF,KAAI,OAAO,qBAAqB,SAC9B,QAAO;AAGT,QAAO,iBAAiB;;AAG1B,SAAgB,yBAAyB,MAAyB;AAChE,KAAI,OAAO,SAAS,SAClB,QAAO;EAAE,MAAM;EAAM,aAAa,KAAA;EAAW;AAG/C,QAAO"}
|
|
1
|
+
{"version":3,"file":"manifest.cjs","names":[],"sources":["../../src/manifest.ts"],"sourcesContent":["export type AssetCrossOrigin = 'anonymous' | 'use-credentials'\n\nexport type AssetCrossOriginConfig =\n | AssetCrossOrigin\n | Partial<Record<'modulepreload' | 'stylesheet', AssetCrossOrigin>>\n\nexport type ManifestAssetLink =\n | string\n | {\n href: string\n crossOrigin?: AssetCrossOrigin\n }\n\nexport function getAssetCrossOrigin(\n assetCrossOrigin: AssetCrossOriginConfig | undefined,\n kind: 'modulepreload' | 'stylesheet',\n): AssetCrossOrigin | undefined {\n if (!assetCrossOrigin) {\n return undefined\n }\n\n if (typeof assetCrossOrigin === 'string') {\n return assetCrossOrigin\n }\n\n return assetCrossOrigin[kind]\n}\n\nexport function resolveManifestAssetLink(link: ManifestAssetLink) {\n if (typeof link === 'string') {\n return { href: link, crossOrigin: undefined }\n }\n\n return link\n}\n\nexport type Manifest = {\n inlineCss?: {\n styles: Record<string, string>\n }\n routes: Record<\n string,\n {\n filePath?: string\n preloads?: Array<ManifestAssetLink>\n assets?: Array<RouterManagedTag>\n }\n >\n}\n\nexport type RouterManagedTag =\n | {\n tag: 'title'\n attrs?: Record<string, any>\n children: string\n }\n | {\n tag: 'meta' | 'link'\n attrs?: Record<string, any>\n children?: never\n }\n | {\n tag: 'script'\n attrs?: Record<string, any>\n children?: string\n }\n | {\n tag: 'style'\n attrs?: Record<string, any>\n children?: string\n inlineCss?: true\n }\n\nexport function getStylesheetHref(asset: RouterManagedTag) {\n if (asset.tag !== 'link') return undefined\n\n const rel = asset.attrs?.rel\n const href = asset.attrs?.href\n if (typeof href !== 'string') return undefined\n\n const relTokens = typeof rel === 'string' ? rel.split(/\\s+/) : []\n if (!relTokens.includes('stylesheet')) return undefined\n\n return href\n}\n\nexport function isInlinableStylesheet(\n manifest: Manifest | undefined,\n asset: RouterManagedTag,\n) {\n const href = getStylesheetHref(asset)\n return !!href && manifest?.inlineCss?.styles[href] !== undefined\n}\n\nexport function createInlineCssStyleAsset(css: string): RouterManagedTag {\n return {\n tag: 'style',\n attrs: {\n suppressHydrationWarning: true,\n },\n inlineCss: true,\n children: css,\n }\n}\n\nexport function createInlineCssPlaceholderAsset(): RouterManagedTag {\n return {\n tag: 'style',\n attrs: {\n suppressHydrationWarning: true,\n },\n inlineCss: true,\n }\n}\n"],"mappings":";AAaA,SAAgB,oBACd,kBACA,MAC8B;AAC9B,KAAI,CAAC,iBACH;AAGF,KAAI,OAAO,qBAAqB,SAC9B,QAAO;AAGT,QAAO,iBAAiB;;AAG1B,SAAgB,yBAAyB,MAAyB;AAChE,KAAI,OAAO,SAAS,SAClB,QAAO;EAAE,MAAM;EAAM,aAAa,KAAA;EAAW;AAG/C,QAAO;;AAwCT,SAAgB,kBAAkB,OAAyB;AACzD,KAAI,MAAM,QAAQ,OAAQ,QAAO,KAAA;CAEjC,MAAM,MAAM,MAAM,OAAO;CACzB,MAAM,OAAO,MAAM,OAAO;AAC1B,KAAI,OAAO,SAAS,SAAU,QAAO,KAAA;AAGrC,KAAI,EADc,OAAO,QAAQ,WAAW,IAAI,MAAM,MAAM,GAAG,EAAE,EAClD,SAAS,aAAa,CAAE,QAAO,KAAA;AAE9C,QAAO;;AAGT,SAAgB,sBACd,UACA,OACA;CACA,MAAM,OAAO,kBAAkB,MAAM;AACrC,QAAO,CAAC,CAAC,QAAQ,UAAU,WAAW,OAAO,UAAU,KAAA;;AAGzD,SAAgB,0BAA0B,KAA+B;AACvE,QAAO;EACL,KAAK;EACL,OAAO,EACL,0BAA0B,MAC3B;EACD,WAAW;EACX,UAAU;EACX;;AAGH,SAAgB,kCAAoD;AAClE,QAAO;EACL,KAAK;EACL,OAAO,EACL,0BAA0B,MAC3B;EACD,WAAW;EACZ"}
|
package/dist/cjs/manifest.d.cts
CHANGED
|
@@ -10,6 +10,9 @@ export declare function resolveManifestAssetLink(link: ManifestAssetLink): {
|
|
|
10
10
|
crossOrigin?: AssetCrossOrigin;
|
|
11
11
|
};
|
|
12
12
|
export type Manifest = {
|
|
13
|
+
inlineCss?: {
|
|
14
|
+
styles: Record<string, string>;
|
|
15
|
+
};
|
|
13
16
|
routes: Record<string, {
|
|
14
17
|
filePath?: string;
|
|
15
18
|
preloads?: Array<ManifestAssetLink>;
|
|
@@ -32,4 +35,9 @@ export type RouterManagedTag = {
|
|
|
32
35
|
tag: 'style';
|
|
33
36
|
attrs?: Record<string, any>;
|
|
34
37
|
children?: string;
|
|
38
|
+
inlineCss?: true;
|
|
35
39
|
};
|
|
40
|
+
export declare function getStylesheetHref(asset: RouterManagedTag): string | undefined;
|
|
41
|
+
export declare function isInlinableStylesheet(manifest: Manifest | undefined, asset: RouterManagedTag): boolean;
|
|
42
|
+
export declare function createInlineCssStyleAsset(css: string): RouterManagedTag;
|
|
43
|
+
export declare function createInlineCssPlaceholderAsset(): RouterManagedTag;
|
|
@@ -2,6 +2,7 @@ const require_utils = require("../utils.cjs");
|
|
|
2
2
|
const require_invariant = require("../invariant.cjs");
|
|
3
3
|
const require_lru_cache = require("../lru-cache.cjs");
|
|
4
4
|
const require_root = require("../root.cjs");
|
|
5
|
+
const require_manifest = require("../manifest.cjs");
|
|
5
6
|
const require_constants = require("./constants.cjs");
|
|
6
7
|
const require_transformer = require("./serializer/transformer.cjs");
|
|
7
8
|
const require_seroval_plugins = require("./serializer/seroval-plugins.cjs");
|
|
@@ -95,6 +96,7 @@ var ScriptBuffer = class {
|
|
|
95
96
|
var isProd = process.env.NODE_ENV === "production";
|
|
96
97
|
var MANIFEST_CACHE_SIZE = 100;
|
|
97
98
|
var manifestCaches = /* @__PURE__ */ new WeakMap();
|
|
99
|
+
var inlineCssCaches = /* @__PURE__ */ new WeakMap();
|
|
98
100
|
function getManifestCache(manifest) {
|
|
99
101
|
const cache = manifestCaches.get(manifest);
|
|
100
102
|
if (cache) return cache;
|
|
@@ -102,17 +104,81 @@ function getManifestCache(manifest) {
|
|
|
102
104
|
manifestCaches.set(manifest, newCache);
|
|
103
105
|
return newCache;
|
|
104
106
|
}
|
|
107
|
+
function getInlineCssCache(manifest) {
|
|
108
|
+
const cache = inlineCssCaches.get(manifest);
|
|
109
|
+
if (cache) return cache;
|
|
110
|
+
const newCache = require_lru_cache.createLRUCache(MANIFEST_CACHE_SIZE);
|
|
111
|
+
inlineCssCaches.set(manifest, newCache);
|
|
112
|
+
return newCache;
|
|
113
|
+
}
|
|
114
|
+
function getInlineCssHrefsForMatches(manifest, matches) {
|
|
115
|
+
const styles = manifest?.inlineCss?.styles;
|
|
116
|
+
if (!styles) return [];
|
|
117
|
+
const seen = /* @__PURE__ */ new Set();
|
|
118
|
+
const hrefs = [];
|
|
119
|
+
for (const match of matches) {
|
|
120
|
+
const assets = manifest?.routes[match.routeId]?.assets ?? [];
|
|
121
|
+
for (const asset of assets) {
|
|
122
|
+
const href = require_manifest.getStylesheetHref(asset);
|
|
123
|
+
if (!href || seen.has(href) || styles[href] === void 0) continue;
|
|
124
|
+
seen.add(href);
|
|
125
|
+
hrefs.push(href);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return hrefs;
|
|
129
|
+
}
|
|
130
|
+
function getInlineCssForHrefs(manifest, hrefs) {
|
|
131
|
+
const styles = manifest.inlineCss?.styles;
|
|
132
|
+
if (!styles || hrefs.length === 0) return void 0;
|
|
133
|
+
const cacheKey = hrefs.join("\0");
|
|
134
|
+
if (isProd) {
|
|
135
|
+
const cached = getInlineCssCache(manifest).get(cacheKey);
|
|
136
|
+
if (cached !== void 0) return cached;
|
|
137
|
+
}
|
|
138
|
+
const css = hrefs.map((href) => styles[href]).join("");
|
|
139
|
+
if (isProd) getInlineCssCache(manifest).set(cacheKey, css);
|
|
140
|
+
return css;
|
|
141
|
+
}
|
|
142
|
+
function getInlineCssAssetForMatches(manifest, matches) {
|
|
143
|
+
if (!manifest?.inlineCss) return void 0;
|
|
144
|
+
const css = getInlineCssForHrefs(manifest, getInlineCssHrefsForMatches(manifest, matches));
|
|
145
|
+
return css === void 0 ? void 0 : require_manifest.createInlineCssStyleAsset(css);
|
|
146
|
+
}
|
|
147
|
+
function stripInlinedStylesheetAssets(manifest, routes, matches) {
|
|
148
|
+
if (!manifest.inlineCss) return routes;
|
|
149
|
+
const nextRoutes = {};
|
|
150
|
+
for (const [routeId, route] of Object.entries(routes)) {
|
|
151
|
+
const assets = route.assets?.filter((asset) => !require_manifest.isInlinableStylesheet(manifest, asset));
|
|
152
|
+
const nextRoute = { ...route };
|
|
153
|
+
if (assets) if (assets.length > 0) nextRoute.assets = assets;
|
|
154
|
+
else delete nextRoute.assets;
|
|
155
|
+
nextRoutes[routeId] = nextRoute;
|
|
156
|
+
}
|
|
157
|
+
if (getInlineCssAssetForMatches(manifest, matches)) {
|
|
158
|
+
const rootRoute = nextRoutes["__root__"] ?? {};
|
|
159
|
+
nextRoutes[require_root.rootRouteId] = {
|
|
160
|
+
...rootRoute,
|
|
161
|
+
assets: [require_manifest.createInlineCssPlaceholderAsset(), ...rootRoute.assets ?? []]
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
return nextRoutes;
|
|
165
|
+
}
|
|
105
166
|
function attachRouterServerSsrUtils({ router, manifest, getRequestAssets, includeUnmatchedRouteAssets = true }) {
|
|
106
167
|
router.ssr = { get manifest() {
|
|
107
168
|
const requestAssets = getRequestAssets?.();
|
|
108
|
-
|
|
169
|
+
const inlineCssAsset = getInlineCssAssetForMatches(manifest, router.stores.matches.get());
|
|
170
|
+
if (!requestAssets?.length && !inlineCssAsset) return manifest;
|
|
109
171
|
return {
|
|
110
172
|
...manifest,
|
|
111
173
|
routes: {
|
|
112
174
|
...manifest?.routes,
|
|
113
175
|
[require_root.rootRouteId]: {
|
|
114
176
|
...manifest?.routes?.[require_root.rootRouteId],
|
|
115
|
-
assets: [
|
|
177
|
+
assets: [
|
|
178
|
+
...requestAssets ?? [],
|
|
179
|
+
...inlineCssAsset ? [inlineCssAsset] : [],
|
|
180
|
+
...manifest?.routes?.["__root__"]?.assets ?? []
|
|
181
|
+
]
|
|
116
182
|
}
|
|
117
183
|
}
|
|
118
184
|
};
|
|
@@ -156,10 +222,10 @@ function attachRouterServerSsrUtils({ router, manifest, getRequestAssets, includ
|
|
|
156
222
|
if (currentRouteIds.has(routeId)) nextFilteredRoutes[routeId] = routeManifest;
|
|
157
223
|
else if (includeUnmatchedRouteAssets && routeManifest.assets && routeManifest.assets.length > 0) nextFilteredRoutes[routeId] = { assets: routeManifest.assets };
|
|
158
224
|
}
|
|
159
|
-
|
|
160
|
-
|
|
225
|
+
filteredRoutes = stripInlinedStylesheetAssets(manifest, nextFilteredRoutes, matchesToDehydrate);
|
|
226
|
+
if (isProd) getManifestCache(manifest).set(manifestCacheKey, filteredRoutes);
|
|
161
227
|
}
|
|
162
|
-
manifestToDehydrate = { routes: filteredRoutes };
|
|
228
|
+
manifestToDehydrate = { routes: { ...filteredRoutes } };
|
|
163
229
|
if (opts?.requestAssets?.length) {
|
|
164
230
|
const existingRoot = manifestToDehydrate.routes[require_root.rootRouteId];
|
|
165
231
|
manifestToDehydrate.routes[require_root.rootRouteId] = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ssr-server.cjs","names":[],"sources":["../../../src/ssr/ssr-server.ts"],"sourcesContent":["import { crossSerializeStream, getCrossReferenceHeader } from 'seroval'\nimport { invariant } from '../invariant'\nimport { decodePath } from '../utils'\nimport { createLRUCache } from '../lru-cache'\nimport { rootRouteId } from '../root'\nimport minifiedTsrBootStrapScript from './tsrScript?script-string'\nimport { GLOBAL_TSR, TSR_SCRIPT_BARRIER_ID } from './constants'\nimport { dehydrateSsrMatchId } from './ssr-match-id'\nimport { defaultSerovalPlugins } from './serializer/seroval-plugins'\nimport { makeSsrSerovalPlugin } from './serializer/transformer'\nimport type { LRUCache } from '../lru-cache'\nimport type { DehydratedMatch, DehydratedRouter } from './types'\nimport type { AnySerializationAdapter } from './serializer/transformer'\nimport type { AnyRouter } from '../router'\nimport type { AnyRouteMatch } from '../Matches'\nimport type { Manifest, RouterManagedTag } from '../manifest'\n\ndeclare module '../router' {\n interface ServerSsr {\n setRenderFinished: () => void\n cleanup: () => void\n }\n interface RouterEvents {\n onInjectedHtml: {\n type: 'onInjectedHtml'\n }\n onSerializationFinished: {\n type: 'onSerializationFinished'\n }\n }\n}\n\nconst SCOPE_ID = 'tsr'\n\nconst TSR_PREFIX = GLOBAL_TSR + '.router='\nconst P_PREFIX = GLOBAL_TSR + '.p(()=>'\nconst P_SUFFIX = ')'\n\nexport function dehydrateMatch(match: AnyRouteMatch): DehydratedMatch {\n const dehydratedMatch: DehydratedMatch = {\n i: dehydrateSsrMatchId(match.id),\n u: match.updatedAt,\n s: match.status,\n }\n\n const properties = [\n ['__beforeLoadContext', 'b'],\n ['loaderData', 'l'],\n ['error', 'e'],\n ['ssr', 'ssr'],\n ] as const\n\n for (const [key, shorthand] of properties) {\n if (match[key] !== undefined) {\n dehydratedMatch[shorthand] = match[key]\n }\n }\n if (match.globalNotFound) {\n dehydratedMatch.g = true\n }\n return dehydratedMatch\n}\n\nconst INITIAL_SCRIPTS = [\n getCrossReferenceHeader(SCOPE_ID),\n minifiedTsrBootStrapScript,\n]\n\nclass ScriptBuffer {\n private router: AnyRouter | undefined\n private _queue: Array<string>\n private _scriptBarrierLifted = false\n private _cleanedUp = false\n private _pendingMicrotask = false\n\n constructor(router: AnyRouter) {\n this.router = router\n // Copy INITIAL_SCRIPTS to avoid mutating the shared array\n this._queue = INITIAL_SCRIPTS.slice()\n }\n\n enqueue(script: string) {\n if (this._cleanedUp) return\n this._queue.push(script)\n // If barrier is lifted, schedule injection (if not already scheduled)\n if (this._scriptBarrierLifted && !this._pendingMicrotask) {\n this._pendingMicrotask = true\n queueMicrotask(() => {\n this._pendingMicrotask = false\n this.injectBufferedScripts()\n })\n }\n }\n\n liftBarrier() {\n if (this._scriptBarrierLifted || this._cleanedUp) return\n this._scriptBarrierLifted = true\n if (this._queue.length > 0 && !this._pendingMicrotask) {\n this._pendingMicrotask = true\n queueMicrotask(() => {\n this._pendingMicrotask = false\n this.injectBufferedScripts()\n })\n }\n }\n\n /**\n * Flushes any pending scripts synchronously.\n * Call this before emitting onSerializationFinished to ensure all scripts are injected.\n *\n * IMPORTANT: Only injects if the barrier has been lifted. Before the barrier is lifted,\n * scripts should remain in the queue so takeBufferedScripts() can retrieve them\n */\n flush() {\n if (!this._scriptBarrierLifted) return\n if (this._cleanedUp) return\n this._pendingMicrotask = false\n const scriptsToInject = this.takeAll()\n if (scriptsToInject && this.router?.serverSsr) {\n this.router.serverSsr.injectScript(scriptsToInject)\n }\n }\n\n takeAll() {\n const bufferedScripts = this._queue\n this._queue = []\n if (bufferedScripts.length === 0) {\n return undefined\n }\n // Optimization: if only one script, avoid join\n if (bufferedScripts.length === 1) {\n return bufferedScripts[0] + ';document.currentScript.remove()'\n }\n // Append cleanup script and join - avoid push() to not mutate then iterate\n return bufferedScripts.join(';') + ';document.currentScript.remove()'\n }\n\n injectBufferedScripts() {\n if (this._cleanedUp) return\n // Early return if queue is empty (avoids unnecessary takeAll() call)\n if (this._queue.length === 0) return\n const scriptsToInject = this.takeAll()\n if (scriptsToInject && this.router?.serverSsr) {\n this.router.serverSsr.injectScript(scriptsToInject)\n }\n }\n\n cleanup() {\n this._cleanedUp = true\n this._queue = []\n this.router = undefined\n }\n}\n\nconst isProd = process.env.NODE_ENV === 'production'\n\ntype FilteredRoutes = Manifest['routes']\n\ntype ManifestLRU = LRUCache<string, FilteredRoutes>\n\nconst MANIFEST_CACHE_SIZE = 100\nconst manifestCaches = new WeakMap<Manifest, ManifestLRU>()\n\nfunction getManifestCache(manifest: Manifest): ManifestLRU {\n const cache = manifestCaches.get(manifest)\n if (cache) return cache\n const newCache = createLRUCache<string, FilteredRoutes>(MANIFEST_CACHE_SIZE)\n manifestCaches.set(manifest, newCache)\n return newCache\n}\n\nexport function attachRouterServerSsrUtils({\n router,\n manifest,\n getRequestAssets,\n includeUnmatchedRouteAssets = true,\n}: {\n router: AnyRouter\n manifest: Manifest | undefined\n getRequestAssets?: () => Array<RouterManagedTag> | undefined\n includeUnmatchedRouteAssets?: boolean\n}) {\n router.ssr = {\n get manifest() {\n const requestAssets = getRequestAssets?.()\n if (!requestAssets?.length) return manifest\n // Merge request-scoped assets into root route without mutating cached manifest\n return {\n ...manifest,\n routes: {\n ...manifest?.routes,\n [rootRouteId]: {\n ...manifest?.routes?.[rootRouteId],\n assets: [\n ...requestAssets,\n ...(manifest?.routes?.[rootRouteId]?.assets ?? []),\n ],\n },\n },\n }\n },\n }\n let _dehydrated = false\n let _serializationFinished = false\n const renderFinishedListeners: Array<() => void> = []\n const serializationFinishedListeners: Array<() => void> = []\n const scriptBuffer = new ScriptBuffer(router)\n let injectedHtmlBuffer = ''\n\n router.serverSsr = {\n injectHtml: (html: string) => {\n if (!html) return\n // Buffer the HTML so it can be retrieved via takeBufferedHtml()\n injectedHtmlBuffer += html\n // Emit event to notify subscribers that new HTML is available\n router.emit({\n type: 'onInjectedHtml',\n })\n },\n injectScript: (script: string) => {\n if (!script) return\n const html = `<script${router.options.ssr?.nonce ? ` nonce='${router.options.ssr.nonce}'` : ''}>${script}</script>`\n router.serverSsr!.injectHtml(html)\n },\n dehydrate: async (opts?: { requestAssets?: Array<RouterManagedTag> }) => {\n if (_dehydrated) {\n if (process.env.NODE_ENV !== 'production') {\n throw new Error('Invariant failed: router is already dehydrated!')\n }\n\n invariant()\n }\n let matchesToDehydrate = router.stores.matches.get()\n if (router.isShell()) {\n // In SPA mode we only want to dehydrate the root match\n matchesToDehydrate = matchesToDehydrate.slice(0, 1)\n }\n const matches = matchesToDehydrate.map(dehydrateMatch)\n\n let manifestToDehydrate: Manifest | undefined = undefined\n // For currently matched routes, send full manifest (preloads + assets).\n // For unmatched routes, include assets only when includeUnmatchedRouteAssets\n // is true; otherwise omit them entirely. Preloads for unmatched routes are\n // still excluded because they are handled via dynamic imports.\n if (manifest) {\n // Prod-only caching; in dev manifests may be replaced/updated (HMR)\n const currentRouteIdsList = matchesToDehydrate.map((m) => m.routeId)\n const manifestCacheKey = `${currentRouteIdsList.join('\\0')}\\0includeUnmatchedRouteAssets=${includeUnmatchedRouteAssets}`\n\n let filteredRoutes: FilteredRoutes | undefined\n\n if (isProd) {\n filteredRoutes = getManifestCache(manifest).get(manifestCacheKey)\n }\n\n if (!filteredRoutes) {\n const currentRouteIds = new Set(currentRouteIdsList)\n const nextFilteredRoutes: FilteredRoutes = {}\n\n for (const routeId in manifest.routes) {\n const routeManifest = manifest.routes[routeId]!\n if (currentRouteIds.has(routeId)) {\n nextFilteredRoutes[routeId] = routeManifest\n } else if (\n includeUnmatchedRouteAssets &&\n routeManifest.assets &&\n routeManifest.assets.length > 0\n ) {\n nextFilteredRoutes[routeId] = {\n assets: routeManifest.assets,\n }\n }\n }\n\n if (isProd) {\n getManifestCache(manifest).set(manifestCacheKey, nextFilteredRoutes)\n }\n\n filteredRoutes = nextFilteredRoutes\n }\n\n manifestToDehydrate = {\n routes: filteredRoutes,\n }\n\n // Merge request-scoped assets into root route (without mutating cached manifest)\n if (opts?.requestAssets?.length) {\n const existingRoot = manifestToDehydrate.routes[rootRouteId]\n manifestToDehydrate.routes[rootRouteId] = {\n ...existingRoot,\n assets: [...opts.requestAssets, ...(existingRoot?.assets ?? [])],\n }\n }\n }\n const dehydratedRouter: DehydratedRouter = {\n manifest: manifestToDehydrate,\n matches,\n }\n const lastMatchId = matchesToDehydrate[matchesToDehydrate.length - 1]?.id\n if (lastMatchId) {\n dehydratedRouter.lastMatchId = dehydrateSsrMatchId(lastMatchId)\n }\n const dehydratedData = await router.options.dehydrate?.()\n if (dehydratedData) {\n dehydratedRouter.dehydratedData = dehydratedData\n }\n _dehydrated = true\n\n const trackPlugins = { didRun: false }\n const serializationAdapters = router.options.serializationAdapters as\n | Array<AnySerializationAdapter>\n | undefined\n const plugins = serializationAdapters\n ? serializationAdapters\n .map((t) => makeSsrSerovalPlugin(t, trackPlugins))\n .concat(defaultSerovalPlugins)\n : defaultSerovalPlugins\n\n const signalSerializationComplete = () => {\n _serializationFinished = true\n try {\n serializationFinishedListeners.forEach((l) => l())\n router.emit({ type: 'onSerializationFinished' })\n } catch (err) {\n console.error('Serialization listener error:', err)\n } finally {\n serializationFinishedListeners.length = 0\n renderFinishedListeners.length = 0\n }\n }\n\n crossSerializeStream(dehydratedRouter, {\n refs: new Map(),\n plugins,\n onSerialize: (data, initial) => {\n let serialized = initial ? TSR_PREFIX + data : data\n if (trackPlugins.didRun) {\n serialized = P_PREFIX + serialized + P_SUFFIX\n }\n scriptBuffer.enqueue(serialized)\n },\n onError: (err: unknown) => {\n console.error('Serialization error:', err)\n if (err && (err as any).stack) {\n console.error((err as any).stack)\n }\n signalSerializationComplete()\n },\n scopeId: SCOPE_ID,\n onDone: () => {\n scriptBuffer.enqueue(GLOBAL_TSR + '.e()')\n // Flush all pending scripts synchronously before signaling completion\n // This ensures all scripts are injected before onSerializationFinished is emitted\n scriptBuffer.flush()\n signalSerializationComplete()\n },\n })\n },\n isDehydrated() {\n return _dehydrated\n },\n isSerializationFinished() {\n return _serializationFinished\n },\n onRenderFinished: (listener) => renderFinishedListeners.push(listener),\n onSerializationFinished: (listener) =>\n serializationFinishedListeners.push(listener),\n setRenderFinished: () => {\n // Wrap in try-catch to ensure scriptBuffer.liftBarrier() is always called\n try {\n renderFinishedListeners.forEach((l) => l())\n } catch (err) {\n console.error('Error in render finished listener:', err)\n } finally {\n // Clear listeners after calling them to prevent memory leaks\n renderFinishedListeners.length = 0\n }\n scriptBuffer.liftBarrier()\n },\n takeBufferedScripts() {\n const scripts = scriptBuffer.takeAll()\n const serverBufferedScript: RouterManagedTag = {\n tag: 'script',\n attrs: {\n nonce: router.options.ssr?.nonce,\n className: '$tsr',\n id: TSR_SCRIPT_BARRIER_ID,\n },\n children: scripts,\n }\n return serverBufferedScript\n },\n liftScriptBarrier() {\n scriptBuffer.liftBarrier()\n },\n takeBufferedHtml() {\n if (!injectedHtmlBuffer) {\n return undefined\n }\n const buffered = injectedHtmlBuffer\n injectedHtmlBuffer = ''\n return buffered\n },\n cleanup() {\n // Guard against multiple cleanup calls\n if (!router.serverSsr) return\n renderFinishedListeners.length = 0\n serializationFinishedListeners.length = 0\n injectedHtmlBuffer = ''\n scriptBuffer.cleanup()\n router.serverSsr = undefined\n },\n }\n}\n\n/**\n * Get the origin for the request.\n *\n * SECURITY: We intentionally do NOT trust the Origin header for determining\n * the router's origin. The Origin header can be spoofed by attackers, which\n * could lead to SSRF-like vulnerabilities where redirects are constructed\n * using a malicious origin (CVE-2024-34351).\n *\n * Instead, we derive the origin from request.url, which is typically set by\n * the server infrastructure (not client-controlled headers).\n *\n * For applications behind proxies that need to trust forwarded headers,\n * use the router's `origin` option to explicitly configure a trusted origin.\n */\nexport function getOrigin(request: Request) {\n try {\n return new URL(request.url).origin\n } catch {}\n return 'http://localhost'\n}\n\n// server and browser can decode/encode characters differently in paths and search params.\n// Server generally strictly follows the WHATWG URL Standard, while browsers may differ for legacy reasons.\n// for example, in paths \"|\" is not encoded on the server but is encoded on chromium (and not on firefox) while \"대\" is encoded on both sides.\n// Another anomaly is that in Node new URLSearchParams and new URL also decode/encode characters differently.\n// new URLSearchParams() encodes \"|\" while new URL() does not, and in this instance\n// chromium treats search params differently than paths, i.e. \"|\" is not encoded in search params.\nexport function getNormalizedURL(url: string | URL, base?: string | URL) {\n // ensure backslashes are encoded correctly in the URL\n if (typeof url === 'string') url = url.replace('\\\\', '%5C')\n\n const rawUrl = new URL(url, base)\n const { path: decodedPathname, handledProtocolRelativeURL } = decodePath(\n rawUrl.pathname,\n )\n const searchParams = new URLSearchParams(rawUrl.search)\n const normalizedHref =\n decodedPathname +\n (searchParams.size > 0 ? '?' : '') +\n searchParams.toString() +\n rawUrl.hash\n\n return {\n url: new URL(normalizedHref, rawUrl.origin),\n handledProtocolRelativeURL,\n }\n}\n"],"mappings":";;;;;;;;;;;AAgCA,IAAM,WAAW;AAEjB,IAAM,aAAa,kBAAA,aAAa;AAChC,IAAM,WAAW,kBAAA,aAAa;AAC9B,IAAM,WAAW;AAEjB,SAAgB,eAAe,OAAuC;CACpE,MAAM,kBAAmC;EACvC,GAAG,qBAAA,oBAAoB,MAAM,GAAG;EAChC,GAAG,MAAM;EACT,GAAG,MAAM;EACV;AASD,MAAK,MAAM,CAAC,KAAK,c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/C,yBAAyB;AAEvB,OAAI;AACF,4BAAwB,SAAS,MAAM,GAAG,CAAC;YACpC,KAAK;AACZ,YAAQ,MAAM,sCAAsC,IAAI;aAChD;AAER,4BAAwB,SAAS;;AAEnC,gBAAa,aAAa;;EAE5B,sBAAsB;GACpB,MAAM,UAAU,aAAa,SAAS;AAUtC,UAT+C;IAC7C,KAAK;IACL,OAAO;KACL,OAAO,OAAO,QAAQ,KAAK;KAC3B,WAAW;KACX,IAAI,kBAAA;KACL;IACD,UAAU;IACX;;EAGH,oBAAoB;AAClB,gBAAa,aAAa;;EAE5B,mBAAmB;AACjB,OAAI,CAAC,mBACH;GAEF,MAAM,WAAW;AACjB,wBAAqB;AACrB,UAAO;;EAET,UAAU;AAER,OAAI,CAAC,OAAO,UAAW;AACvB,2BAAwB,SAAS;AACjC,kCAA+B,SAAS;AACxC,wBAAqB;AACrB,gBAAa,SAAS;AACtB,UAAO,YAAY,KAAA;;EAEtB;;;;;;;;;;;;;;;;AAiBH,SAAgB,UAAU,SAAkB;AAC1C,KAAI;AACF,SAAO,IAAI,IAAI,QAAQ,IAAI,CAAC;SACtB;AACR,QAAO;;AAST,SAAgB,iBAAiB,KAAmB,MAAqB;AAEvE,KAAI,OAAO,QAAQ,SAAU,OAAM,IAAI,QAAQ,MAAM,MAAM;CAE3D,MAAM,SAAS,IAAI,IAAI,KAAK,KAAK;CACjC,MAAM,EAAE,MAAM,iBAAiB,+BAA+B,cAAA,WAC5D,OAAO,SACR;CACD,MAAM,eAAe,IAAI,gBAAgB,OAAO,OAAO;CACvD,MAAM,iBACJ,mBACC,aAAa,OAAO,IAAI,MAAM,MAC/B,aAAa,UAAU,GACvB,OAAO;AAET,QAAO;EACL,KAAK,IAAI,IAAI,gBAAgB,OAAO,OAAO;EAC3C;EACD"}
|
|
1
|
+
{"version":3,"file":"ssr-server.cjs","names":[],"sources":["../../../src/ssr/ssr-server.ts"],"sourcesContent":["import { crossSerializeStream, getCrossReferenceHeader } from 'seroval'\nimport { invariant } from '../invariant'\nimport {\n createInlineCssPlaceholderAsset,\n createInlineCssStyleAsset,\n getStylesheetHref,\n isInlinableStylesheet,\n} from '../manifest'\nimport { decodePath } from '../utils'\nimport { createLRUCache } from '../lru-cache'\nimport { rootRouteId } from '../root'\nimport minifiedTsrBootStrapScript from './tsrScript?script-string'\nimport { GLOBAL_TSR, TSR_SCRIPT_BARRIER_ID } from './constants'\nimport { dehydrateSsrMatchId } from './ssr-match-id'\nimport { defaultSerovalPlugins } from './serializer/seroval-plugins'\nimport { makeSsrSerovalPlugin } from './serializer/transformer'\nimport type { LRUCache } from '../lru-cache'\nimport type { DehydratedMatch, DehydratedRouter } from './types'\nimport type { AnySerializationAdapter } from './serializer/transformer'\nimport type { AnyRouter } from '../router'\nimport type { AnyRouteMatch } from '../Matches'\nimport type { Manifest, RouterManagedTag } from '../manifest'\n\ndeclare module '../router' {\n interface ServerSsr {\n setRenderFinished: () => void\n cleanup: () => void\n }\n interface RouterEvents {\n onInjectedHtml: {\n type: 'onInjectedHtml'\n }\n onSerializationFinished: {\n type: 'onSerializationFinished'\n }\n }\n}\n\nconst SCOPE_ID = 'tsr'\n\nconst TSR_PREFIX = GLOBAL_TSR + '.router='\nconst P_PREFIX = GLOBAL_TSR + '.p(()=>'\nconst P_SUFFIX = ')'\n\nexport function dehydrateMatch(match: AnyRouteMatch): DehydratedMatch {\n const dehydratedMatch: DehydratedMatch = {\n i: dehydrateSsrMatchId(match.id),\n u: match.updatedAt,\n s: match.status,\n }\n\n const properties = [\n ['__beforeLoadContext', 'b'],\n ['loaderData', 'l'],\n ['error', 'e'],\n ['ssr', 'ssr'],\n ] as const\n\n for (const [key, shorthand] of properties) {\n if (match[key] !== undefined) {\n dehydratedMatch[shorthand] = match[key]\n }\n }\n if (match.globalNotFound) {\n dehydratedMatch.g = true\n }\n return dehydratedMatch\n}\n\nconst INITIAL_SCRIPTS = [\n getCrossReferenceHeader(SCOPE_ID),\n minifiedTsrBootStrapScript,\n]\n\nclass ScriptBuffer {\n private router: AnyRouter | undefined\n private _queue: Array<string>\n private _scriptBarrierLifted = false\n private _cleanedUp = false\n private _pendingMicrotask = false\n\n constructor(router: AnyRouter) {\n this.router = router\n // Copy INITIAL_SCRIPTS to avoid mutating the shared array\n this._queue = INITIAL_SCRIPTS.slice()\n }\n\n enqueue(script: string) {\n if (this._cleanedUp) return\n this._queue.push(script)\n // If barrier is lifted, schedule injection (if not already scheduled)\n if (this._scriptBarrierLifted && !this._pendingMicrotask) {\n this._pendingMicrotask = true\n queueMicrotask(() => {\n this._pendingMicrotask = false\n this.injectBufferedScripts()\n })\n }\n }\n\n liftBarrier() {\n if (this._scriptBarrierLifted || this._cleanedUp) return\n this._scriptBarrierLifted = true\n if (this._queue.length > 0 && !this._pendingMicrotask) {\n this._pendingMicrotask = true\n queueMicrotask(() => {\n this._pendingMicrotask = false\n this.injectBufferedScripts()\n })\n }\n }\n\n /**\n * Flushes any pending scripts synchronously.\n * Call this before emitting onSerializationFinished to ensure all scripts are injected.\n *\n * IMPORTANT: Only injects if the barrier has been lifted. Before the barrier is lifted,\n * scripts should remain in the queue so takeBufferedScripts() can retrieve them\n */\n flush() {\n if (!this._scriptBarrierLifted) return\n if (this._cleanedUp) return\n this._pendingMicrotask = false\n const scriptsToInject = this.takeAll()\n if (scriptsToInject && this.router?.serverSsr) {\n this.router.serverSsr.injectScript(scriptsToInject)\n }\n }\n\n takeAll() {\n const bufferedScripts = this._queue\n this._queue = []\n if (bufferedScripts.length === 0) {\n return undefined\n }\n // Optimization: if only one script, avoid join\n if (bufferedScripts.length === 1) {\n return bufferedScripts[0] + ';document.currentScript.remove()'\n }\n // Append cleanup script and join - avoid push() to not mutate then iterate\n return bufferedScripts.join(';') + ';document.currentScript.remove()'\n }\n\n injectBufferedScripts() {\n if (this._cleanedUp) return\n // Early return if queue is empty (avoids unnecessary takeAll() call)\n if (this._queue.length === 0) return\n const scriptsToInject = this.takeAll()\n if (scriptsToInject && this.router?.serverSsr) {\n this.router.serverSsr.injectScript(scriptsToInject)\n }\n }\n\n cleanup() {\n this._cleanedUp = true\n this._queue = []\n this.router = undefined\n }\n}\n\nconst isProd = process.env.NODE_ENV === 'production'\n\ntype FilteredRoutes = Manifest['routes']\n\ntype ManifestLRU = LRUCache<string, FilteredRoutes>\ntype InlineCssLRU = LRUCache<string, string>\n\nconst MANIFEST_CACHE_SIZE = 100\nconst manifestCaches = new WeakMap<Manifest, ManifestLRU>()\nconst inlineCssCaches = new WeakMap<Manifest, InlineCssLRU>()\n\nfunction getManifestCache(manifest: Manifest): ManifestLRU {\n const cache = manifestCaches.get(manifest)\n if (cache) return cache\n const newCache = createLRUCache<string, FilteredRoutes>(MANIFEST_CACHE_SIZE)\n manifestCaches.set(manifest, newCache)\n return newCache\n}\n\nfunction getInlineCssCache(manifest: Manifest): InlineCssLRU {\n const cache = inlineCssCaches.get(manifest)\n if (cache) return cache\n const newCache = createLRUCache<string, string>(MANIFEST_CACHE_SIZE)\n inlineCssCaches.set(manifest, newCache)\n return newCache\n}\n\nfunction getInlineCssHrefsForMatches(\n manifest: Manifest | undefined,\n matches: Array<AnyRouteMatch>,\n) {\n const styles = manifest?.inlineCss?.styles\n if (!styles) return []\n\n const seen = new Set<string>()\n const hrefs: Array<string> = []\n\n for (const match of matches) {\n const assets = manifest?.routes[match.routeId]?.assets ?? []\n for (const asset of assets) {\n const href = getStylesheetHref(asset)\n if (!href || seen.has(href) || styles[href] === undefined) {\n continue\n }\n seen.add(href)\n hrefs.push(href)\n }\n }\n\n return hrefs\n}\n\nfunction getInlineCssForHrefs(manifest: Manifest, hrefs: Array<string>) {\n const styles = manifest.inlineCss?.styles\n if (!styles || hrefs.length === 0) return undefined\n\n const cacheKey = hrefs.join('\\0')\n if (isProd) {\n const cached = getInlineCssCache(manifest).get(cacheKey)\n if (cached !== undefined) return cached\n }\n\n const css = hrefs.map((href) => styles[href]!).join('')\n\n if (isProd) {\n getInlineCssCache(manifest).set(cacheKey, css)\n }\n\n return css\n}\n\nfunction getInlineCssAssetForMatches(\n manifest: Manifest | undefined,\n matches: Array<AnyRouteMatch>,\n) {\n if (!manifest?.inlineCss) return undefined\n\n const hrefs = getInlineCssHrefsForMatches(manifest, matches)\n const css = getInlineCssForHrefs(manifest, hrefs)\n\n return css === undefined ? undefined : createInlineCssStyleAsset(css)\n}\n\nfunction stripInlinedStylesheetAssets(\n manifest: Manifest,\n routes: FilteredRoutes,\n matches: Array<AnyRouteMatch>,\n): FilteredRoutes {\n if (!manifest.inlineCss) {\n return routes\n }\n\n const nextRoutes: FilteredRoutes = {}\n\n for (const [routeId, route] of Object.entries(routes)) {\n const assets = route.assets?.filter(\n (asset) => !isInlinableStylesheet(manifest, asset),\n )\n\n const nextRoute = { ...route }\n if (assets) {\n if (assets.length > 0) {\n nextRoute.assets = assets\n } else {\n delete nextRoute.assets\n }\n }\n nextRoutes[routeId] = nextRoute\n }\n\n if (getInlineCssAssetForMatches(manifest, matches)) {\n const rootRoute = nextRoutes[rootRouteId] ?? {}\n nextRoutes[rootRouteId] = {\n ...rootRoute,\n assets: [createInlineCssPlaceholderAsset(), ...(rootRoute.assets ?? [])],\n }\n }\n\n return nextRoutes\n}\n\nexport function attachRouterServerSsrUtils({\n router,\n manifest,\n getRequestAssets,\n includeUnmatchedRouteAssets = true,\n}: {\n router: AnyRouter\n manifest: Manifest | undefined\n getRequestAssets?: () => Array<RouterManagedTag> | undefined\n includeUnmatchedRouteAssets?: boolean\n}) {\n router.ssr = {\n get manifest() {\n const requestAssets = getRequestAssets?.()\n const inlineCssAsset = getInlineCssAssetForMatches(\n manifest,\n router.stores.matches.get(),\n )\n if (!requestAssets?.length && !inlineCssAsset) return manifest\n // Merge request-scoped assets into root route without mutating cached manifest\n return {\n ...manifest,\n routes: {\n ...manifest?.routes,\n [rootRouteId]: {\n ...manifest?.routes?.[rootRouteId],\n assets: [\n ...(requestAssets ?? []),\n ...(inlineCssAsset ? [inlineCssAsset] : []),\n ...(manifest?.routes?.[rootRouteId]?.assets ?? []),\n ],\n },\n },\n }\n },\n }\n let _dehydrated = false\n let _serializationFinished = false\n const renderFinishedListeners: Array<() => void> = []\n const serializationFinishedListeners: Array<() => void> = []\n const scriptBuffer = new ScriptBuffer(router)\n let injectedHtmlBuffer = ''\n\n router.serverSsr = {\n injectHtml: (html: string) => {\n if (!html) return\n // Buffer the HTML so it can be retrieved via takeBufferedHtml()\n injectedHtmlBuffer += html\n // Emit event to notify subscribers that new HTML is available\n router.emit({\n type: 'onInjectedHtml',\n })\n },\n injectScript: (script: string) => {\n if (!script) return\n const html = `<script${router.options.ssr?.nonce ? ` nonce='${router.options.ssr.nonce}'` : ''}>${script}</script>`\n router.serverSsr!.injectHtml(html)\n },\n dehydrate: async (opts?: { requestAssets?: Array<RouterManagedTag> }) => {\n if (_dehydrated) {\n if (process.env.NODE_ENV !== 'production') {\n throw new Error('Invariant failed: router is already dehydrated!')\n }\n\n invariant()\n }\n let matchesToDehydrate = router.stores.matches.get()\n if (router.isShell()) {\n // In SPA mode we only want to dehydrate the root match\n matchesToDehydrate = matchesToDehydrate.slice(0, 1)\n }\n const matches = matchesToDehydrate.map(dehydrateMatch)\n\n let manifestToDehydrate: Manifest | undefined = undefined\n // For currently matched routes, send full manifest (preloads + assets).\n // For unmatched routes, include assets only when includeUnmatchedRouteAssets\n // is true; otherwise omit them entirely. Preloads for unmatched routes are\n // still excluded because they are handled via dynamic imports.\n if (manifest) {\n // Prod-only caching; in dev manifests may be replaced/updated (HMR)\n const currentRouteIdsList = matchesToDehydrate.map((m) => m.routeId)\n const manifestCacheKey = `${currentRouteIdsList.join('\\0')}\\0includeUnmatchedRouteAssets=${includeUnmatchedRouteAssets}`\n\n let filteredRoutes: FilteredRoutes | undefined\n\n if (isProd) {\n filteredRoutes = getManifestCache(manifest).get(manifestCacheKey)\n }\n\n if (!filteredRoutes) {\n const currentRouteIds = new Set(currentRouteIdsList)\n const nextFilteredRoutes: FilteredRoutes = {}\n\n for (const routeId in manifest.routes) {\n const routeManifest = manifest.routes[routeId]!\n if (currentRouteIds.has(routeId)) {\n nextFilteredRoutes[routeId] = routeManifest\n } else if (\n includeUnmatchedRouteAssets &&\n routeManifest.assets &&\n routeManifest.assets.length > 0\n ) {\n nextFilteredRoutes[routeId] = {\n assets: routeManifest.assets,\n }\n }\n }\n\n filteredRoutes = stripInlinedStylesheetAssets(\n manifest,\n nextFilteredRoutes,\n matchesToDehydrate,\n )\n\n if (isProd) {\n getManifestCache(manifest).set(manifestCacheKey, filteredRoutes)\n }\n }\n\n manifestToDehydrate = {\n routes: { ...filteredRoutes },\n }\n\n // Merge request-scoped assets into root route (without mutating cached manifest)\n if (opts?.requestAssets?.length) {\n const existingRoot = manifestToDehydrate.routes[rootRouteId]\n manifestToDehydrate.routes[rootRouteId] = {\n ...existingRoot,\n assets: [...opts.requestAssets, ...(existingRoot?.assets ?? [])],\n }\n }\n }\n const dehydratedRouter: DehydratedRouter = {\n manifest: manifestToDehydrate,\n matches,\n }\n const lastMatchId = matchesToDehydrate[matchesToDehydrate.length - 1]?.id\n if (lastMatchId) {\n dehydratedRouter.lastMatchId = dehydrateSsrMatchId(lastMatchId)\n }\n const dehydratedData = await router.options.dehydrate?.()\n if (dehydratedData) {\n dehydratedRouter.dehydratedData = dehydratedData\n }\n _dehydrated = true\n\n const trackPlugins = { didRun: false }\n const serializationAdapters = router.options.serializationAdapters as\n | Array<AnySerializationAdapter>\n | undefined\n const plugins = serializationAdapters\n ? serializationAdapters\n .map((t) => makeSsrSerovalPlugin(t, trackPlugins))\n .concat(defaultSerovalPlugins)\n : defaultSerovalPlugins\n\n const signalSerializationComplete = () => {\n _serializationFinished = true\n try {\n serializationFinishedListeners.forEach((l) => l())\n router.emit({ type: 'onSerializationFinished' })\n } catch (err) {\n console.error('Serialization listener error:', err)\n } finally {\n serializationFinishedListeners.length = 0\n renderFinishedListeners.length = 0\n }\n }\n\n crossSerializeStream(dehydratedRouter, {\n refs: new Map(),\n plugins,\n onSerialize: (data, initial) => {\n let serialized = initial ? TSR_PREFIX + data : data\n if (trackPlugins.didRun) {\n serialized = P_PREFIX + serialized + P_SUFFIX\n }\n scriptBuffer.enqueue(serialized)\n },\n onError: (err: unknown) => {\n console.error('Serialization error:', err)\n if (err && (err as any).stack) {\n console.error((err as any).stack)\n }\n signalSerializationComplete()\n },\n scopeId: SCOPE_ID,\n onDone: () => {\n scriptBuffer.enqueue(GLOBAL_TSR + '.e()')\n // Flush all pending scripts synchronously before signaling completion\n // This ensures all scripts are injected before onSerializationFinished is emitted\n scriptBuffer.flush()\n signalSerializationComplete()\n },\n })\n },\n isDehydrated() {\n return _dehydrated\n },\n isSerializationFinished() {\n return _serializationFinished\n },\n onRenderFinished: (listener) => renderFinishedListeners.push(listener),\n onSerializationFinished: (listener) =>\n serializationFinishedListeners.push(listener),\n setRenderFinished: () => {\n // Wrap in try-catch to ensure scriptBuffer.liftBarrier() is always called\n try {\n renderFinishedListeners.forEach((l) => l())\n } catch (err) {\n console.error('Error in render finished listener:', err)\n } finally {\n // Clear listeners after calling them to prevent memory leaks\n renderFinishedListeners.length = 0\n }\n scriptBuffer.liftBarrier()\n },\n takeBufferedScripts() {\n const scripts = scriptBuffer.takeAll()\n const serverBufferedScript: RouterManagedTag = {\n tag: 'script',\n attrs: {\n nonce: router.options.ssr?.nonce,\n className: '$tsr',\n id: TSR_SCRIPT_BARRIER_ID,\n },\n children: scripts,\n }\n return serverBufferedScript\n },\n liftScriptBarrier() {\n scriptBuffer.liftBarrier()\n },\n takeBufferedHtml() {\n if (!injectedHtmlBuffer) {\n return undefined\n }\n const buffered = injectedHtmlBuffer\n injectedHtmlBuffer = ''\n return buffered\n },\n cleanup() {\n // Guard against multiple cleanup calls\n if (!router.serverSsr) return\n renderFinishedListeners.length = 0\n serializationFinishedListeners.length = 0\n injectedHtmlBuffer = ''\n scriptBuffer.cleanup()\n router.serverSsr = undefined\n },\n }\n}\n\n/**\n * Get the origin for the request.\n *\n * SECURITY: We intentionally do NOT trust the Origin header for determining\n * the router's origin. The Origin header can be spoofed by attackers, which\n * could lead to SSRF-like vulnerabilities where redirects are constructed\n * using a malicious origin (CVE-2024-34351).\n *\n * Instead, we derive the origin from request.url, which is typically set by\n * the server infrastructure (not client-controlled headers).\n *\n * For applications behind proxies that need to trust forwarded headers,\n * use the router's `origin` option to explicitly configure a trusted origin.\n */\nexport function getOrigin(request: Request) {\n try {\n return new URL(request.url).origin\n } catch {}\n return 'http://localhost'\n}\n\n// server and browser can decode/encode characters differently in paths and search params.\n// Server generally strictly follows the WHATWG URL Standard, while browsers may differ for legacy reasons.\n// for example, in paths \"|\" is not encoded on the server but is encoded on chromium (and not on firefox) while \"대\" is encoded on both sides.\n// Another anomaly is that in Node new URLSearchParams and new URL also decode/encode characters differently.\n// new URLSearchParams() encodes \"|\" while new URL() does not, and in this instance\n// chromium treats search params differently than paths, i.e. \"|\" is not encoded in search params.\nexport function getNormalizedURL(url: string | URL, base?: string | URL) {\n // ensure backslashes are encoded correctly in the URL\n if (typeof url === 'string') url = url.replace('\\\\', '%5C')\n\n const rawUrl = new URL(url, base)\n const { path: decodedPathname, handledProtocolRelativeURL } = decodePath(\n rawUrl.pathname,\n )\n const searchParams = new URLSearchParams(rawUrl.search)\n const normalizedHref =\n decodedPathname +\n (searchParams.size > 0 ? '?' : '') +\n searchParams.toString() +\n rawUrl.hash\n\n return {\n url: new URL(normalizedHref, rawUrl.origin),\n handledProtocolRelativeURL,\n }\n}\n"],"mappings":";;;;;;;;;;;;AAsCA,IAAM,WAAW;AAEjB,IAAM,aAAa,kBAAA,aAAa;AAChC,IAAM,WAAW,kBAAA,aAAa;AAC9B,IAAM,WAAW;AAEjB,SAAgB,eAAe,OAAuC;CACpE,MAAM,kBAAmC;EACvC,GAAG,qBAAA,oBAAoB,MAAM,GAAG;EAChC,GAAG,MAAM;EACT,GAAG,MAAM;EACV;AASD,MAAK,MAAM,CAAC,KAAK,cAPE;EACjB,CAAC,uBAAuB,IAAI;EAC5B,CAAC,cAAc,IAAI;EACnB,CAAC,SAAS,IA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AACxC,wBAAqB;AACrB,gBAAa,SAAS;AACtB,UAAO,YAAY,KAAA;;EAEtB;;;;;;;;;;;;;;;;AAiBH,SAAgB,UAAU,SAAkB;AAC1C,KAAI;AACF,SAAO,IAAI,IAAI,QAAQ,IAAI,CAAC;SACtB;AACR,QAAO;;AAST,SAAgB,iBAAiB,KAAmB,MAAqB;AAEvE,KAAI,OAAO,QAAQ,SAAU,OAAM,IAAI,QAAQ,MAAM,MAAM;CAE3D,MAAM,SAAS,IAAI,IAAI,KAAK,KAAK;CACjC,MAAM,EAAE,MAAM,iBAAiB,+BAA+B,cAAA,WAC5D,OAAO,SACR;CACD,MAAM,eAAe,IAAI,gBAAgB,OAAO,OAAO;CACvD,MAAM,iBACJ,mBACC,aAAa,OAAO,IAAI,MAAM,MAC/B,aAAa,UAAU,GACvB,OAAO;AAET,QAAO;EACL,KAAK,IAAI,IAAI,gBAAgB,OAAO,OAAO;EAC3C;EACD"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export type { RouteToPath, TrailingSlashOptionByRouter, ParseRoute, CodeRouteToP
|
|
|
8
8
|
export type { InferFileRouteTypes, FileRouteTypes, FileRoutesByPath, CreateFileRoute, LazyRoute, LazyRouteOptions, CreateLazyFileRoute, } from './fileRoute.js';
|
|
9
9
|
export type { ParsedLocation } from './location.js';
|
|
10
10
|
export type { Manifest, RouterManagedTag, AssetCrossOrigin, AssetCrossOriginConfig, ManifestAssetLink, } from './manifest.js';
|
|
11
|
-
export { getAssetCrossOrigin, resolveManifestAssetLink } from './manifest.js';
|
|
11
|
+
export { createInlineCssStyleAsset, getAssetCrossOrigin, getStylesheetHref, isInlinableStylesheet, resolveManifestAssetLink, } from './manifest.js';
|
|
12
12
|
export { isMatch } from './Matches.js';
|
|
13
13
|
export type { AnyMatchAndValue, FindValueByIndex, FindValueByKey, CreateMatchAndValue, NextMatchAndValue, IsMatchKeyOf, IsMatchPath, IsMatchResult, IsMatchParse, IsMatch, RouteMatch, RouteMatchExtensions, MakeRouteMatchUnion, MakeRouteMatch, AnyRouteMatch, MakeRouteMatchFromRoute, MatchRouteOptions, } from './Matches.js';
|
|
14
14
|
export { joinPaths, cleanPath, trimPathLeft, trimPathRight, trimPath, removeTrailingSlash, exactPathTest, resolvePath, interpolatePath, } from './path.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -12,7 +12,7 @@ import { createNonReactiveMutableStore, createNonReactiveReadonlyStore } from ".
|
|
|
12
12
|
import { PathParamError, RouterCore, SearchParamError, defaultSerializeError, getInitialRouterState, getLocationChangeInfo, getMatchedRoutes, lazyFn, trailingSlashOptions } from "./router.js";
|
|
13
13
|
import { TSR_DEFERRED_PROMISE, defer } from "./defer.js";
|
|
14
14
|
import { preloadWarning } from "./link.js";
|
|
15
|
-
import { getAssetCrossOrigin, resolveManifestAssetLink } from "./manifest.js";
|
|
15
|
+
import { createInlineCssStyleAsset, getAssetCrossOrigin, getStylesheetHref, isInlinableStylesheet, resolveManifestAssetLink } from "./manifest.js";
|
|
16
16
|
import { isMatch } from "./Matches.js";
|
|
17
17
|
import { BaseRootRoute, BaseRoute, BaseRouteApi } from "./route.js";
|
|
18
18
|
import { createRouterConfig } from "./config.js";
|
|
@@ -21,4 +21,4 @@ import { handleHashScroll } from "./hash-scroll.js";
|
|
|
21
21
|
import { createSerializationAdapter, makeSerovalPlugin, makeSsrSerovalPlugin } from "./ssr/serializer/transformer.js";
|
|
22
22
|
import { RawStream, createRawStreamDeserializePlugin, createRawStreamRPCPlugin } from "./ssr/serializer/RawStream.js";
|
|
23
23
|
import { defaultSerovalPlugins } from "./ssr/serializer/seroval-plugins.js";
|
|
24
|
-
export { BaseRootRoute, BaseRoute, BaseRouteApi, DEFAULT_PROTOCOL_ALLOWLIST, PathParamError, RawStream, RouterCore, SearchParamError, TSR_DEFERRED_PROMISE, buildDevStylesUrl, cleanPath, composeRewrites, createControlledPromise, createNonReactiveMutableStore, createNonReactiveReadonlyStore, createRawStreamDeserializePlugin, createRawStreamRPCPlugin, createRouterConfig, createSerializationAdapter, decode, deepEqual, defaultGetScrollRestorationKey, defaultParseSearch, defaultSerializeError, defaultSerovalPlugins, defaultStringifySearch, defer, encode, escapeHtml, exactPathTest, executeRewriteInput, functionalUpdate, getAssetCrossOrigin, getElementScrollRestorationEntry, getInitialRouterState, getLocationChangeInfo, getMatchedRoutes, handleHashScroll, interpolatePath, invariant, isDangerousProtocol, isMatch, isModuleNotFoundError, isNotFound, isPlainArray, isPlainObject, isRedirect, isResolvedRedirect, joinPaths, lazyFn, makeSerovalPlugin, makeSsrSerovalPlugin, notFound, parseRedirect, parseSearchWith, preloadWarning, redirect, removeTrailingSlash, replaceEqualDeep, resolveManifestAssetLink, resolvePath, retainSearchParams, rootRouteId, scrollRestorationCache, setupScrollRestoration, storageKey, stringifySearchWith, stripSearchParams, trailingSlashOptions, trimPath, trimPathLeft, trimPathRight };
|
|
24
|
+
export { BaseRootRoute, BaseRoute, BaseRouteApi, DEFAULT_PROTOCOL_ALLOWLIST, PathParamError, RawStream, RouterCore, SearchParamError, TSR_DEFERRED_PROMISE, buildDevStylesUrl, cleanPath, composeRewrites, createControlledPromise, createInlineCssStyleAsset, createNonReactiveMutableStore, createNonReactiveReadonlyStore, createRawStreamDeserializePlugin, createRawStreamRPCPlugin, createRouterConfig, createSerializationAdapter, decode, deepEqual, defaultGetScrollRestorationKey, defaultParseSearch, defaultSerializeError, defaultSerovalPlugins, defaultStringifySearch, defer, encode, escapeHtml, exactPathTest, executeRewriteInput, functionalUpdate, getAssetCrossOrigin, getElementScrollRestorationEntry, getInitialRouterState, getLocationChangeInfo, getMatchedRoutes, getStylesheetHref, handleHashScroll, interpolatePath, invariant, isDangerousProtocol, isInlinableStylesheet, isMatch, isModuleNotFoundError, isNotFound, isPlainArray, isPlainObject, isRedirect, isResolvedRedirect, joinPaths, lazyFn, makeSerovalPlugin, makeSsrSerovalPlugin, notFound, parseRedirect, parseSearchWith, preloadWarning, redirect, removeTrailingSlash, replaceEqualDeep, resolveManifestAssetLink, resolvePath, retainSearchParams, rootRouteId, scrollRestorationCache, setupScrollRestoration, storageKey, stringifySearchWith, stripSearchParams, trailingSlashOptions, trimPath, trimPathLeft, trimPathRight };
|
package/dist/esm/manifest.d.ts
CHANGED
|
@@ -10,6 +10,9 @@ export declare function resolveManifestAssetLink(link: ManifestAssetLink): {
|
|
|
10
10
|
crossOrigin?: AssetCrossOrigin;
|
|
11
11
|
};
|
|
12
12
|
export type Manifest = {
|
|
13
|
+
inlineCss?: {
|
|
14
|
+
styles: Record<string, string>;
|
|
15
|
+
};
|
|
13
16
|
routes: Record<string, {
|
|
14
17
|
filePath?: string;
|
|
15
18
|
preloads?: Array<ManifestAssetLink>;
|
|
@@ -32,4 +35,9 @@ export type RouterManagedTag = {
|
|
|
32
35
|
tag: 'style';
|
|
33
36
|
attrs?: Record<string, any>;
|
|
34
37
|
children?: string;
|
|
38
|
+
inlineCss?: true;
|
|
35
39
|
};
|
|
40
|
+
export declare function getStylesheetHref(asset: RouterManagedTag): string | undefined;
|
|
41
|
+
export declare function isInlinableStylesheet(manifest: Manifest | undefined, asset: RouterManagedTag): boolean;
|
|
42
|
+
export declare function createInlineCssStyleAsset(css: string): RouterManagedTag;
|
|
43
|
+
export declare function createInlineCssPlaceholderAsset(): RouterManagedTag;
|
package/dist/esm/manifest.js
CHANGED
|
@@ -11,7 +11,34 @@ function resolveManifestAssetLink(link) {
|
|
|
11
11
|
};
|
|
12
12
|
return link;
|
|
13
13
|
}
|
|
14
|
+
function getStylesheetHref(asset) {
|
|
15
|
+
if (asset.tag !== "link") return void 0;
|
|
16
|
+
const rel = asset.attrs?.rel;
|
|
17
|
+
const href = asset.attrs?.href;
|
|
18
|
+
if (typeof href !== "string") return void 0;
|
|
19
|
+
if (!(typeof rel === "string" ? rel.split(/\s+/) : []).includes("stylesheet")) return void 0;
|
|
20
|
+
return href;
|
|
21
|
+
}
|
|
22
|
+
function isInlinableStylesheet(manifest, asset) {
|
|
23
|
+
const href = getStylesheetHref(asset);
|
|
24
|
+
return !!href && manifest?.inlineCss?.styles[href] !== void 0;
|
|
25
|
+
}
|
|
26
|
+
function createInlineCssStyleAsset(css) {
|
|
27
|
+
return {
|
|
28
|
+
tag: "style",
|
|
29
|
+
attrs: { suppressHydrationWarning: true },
|
|
30
|
+
inlineCss: true,
|
|
31
|
+
children: css
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function createInlineCssPlaceholderAsset() {
|
|
35
|
+
return {
|
|
36
|
+
tag: "style",
|
|
37
|
+
attrs: { suppressHydrationWarning: true },
|
|
38
|
+
inlineCss: true
|
|
39
|
+
};
|
|
40
|
+
}
|
|
14
41
|
//#endregion
|
|
15
|
-
export { getAssetCrossOrigin, resolveManifestAssetLink };
|
|
42
|
+
export { createInlineCssPlaceholderAsset, createInlineCssStyleAsset, getAssetCrossOrigin, getStylesheetHref, isInlinableStylesheet, resolveManifestAssetLink };
|
|
16
43
|
|
|
17
44
|
//# sourceMappingURL=manifest.js.map
|
package/dist/esm/manifest.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.js","names":[],"sources":["../../src/manifest.ts"],"sourcesContent":["export type AssetCrossOrigin = 'anonymous' | 'use-credentials'\n\nexport type AssetCrossOriginConfig =\n | AssetCrossOrigin\n | Partial<Record<'modulepreload' | 'stylesheet', AssetCrossOrigin>>\n\nexport type ManifestAssetLink =\n | string\n | {\n href: string\n crossOrigin?: AssetCrossOrigin\n }\n\nexport function getAssetCrossOrigin(\n assetCrossOrigin: AssetCrossOriginConfig | undefined,\n kind: 'modulepreload' | 'stylesheet',\n): AssetCrossOrigin | undefined {\n if (!assetCrossOrigin) {\n return undefined\n }\n\n if (typeof assetCrossOrigin === 'string') {\n return assetCrossOrigin\n }\n\n return assetCrossOrigin[kind]\n}\n\nexport function resolveManifestAssetLink(link: ManifestAssetLink) {\n if (typeof link === 'string') {\n return { href: link, crossOrigin: undefined }\n }\n\n return link\n}\n\nexport type Manifest = {\n routes: Record<\n string,\n {\n filePath?: string\n preloads?: Array<ManifestAssetLink>\n assets?: Array<RouterManagedTag>\n }\n >\n}\n\nexport type RouterManagedTag =\n | {\n tag: 'title'\n attrs?: Record<string, any>\n children: string\n }\n | {\n tag: 'meta' | 'link'\n attrs?: Record<string, any>\n children?: never\n }\n | {\n tag: 'script'\n attrs?: Record<string, any>\n children?: string\n }\n | {\n tag: 'style'\n attrs?: Record<string, any>\n children?: string\n }\n"],"mappings":";AAaA,SAAgB,oBACd,kBACA,MAC8B;AAC9B,KAAI,CAAC,iBACH;AAGF,KAAI,OAAO,qBAAqB,SAC9B,QAAO;AAGT,QAAO,iBAAiB;;AAG1B,SAAgB,yBAAyB,MAAyB;AAChE,KAAI,OAAO,SAAS,SAClB,QAAO;EAAE,MAAM;EAAM,aAAa,KAAA;EAAW;AAG/C,QAAO"}
|
|
1
|
+
{"version":3,"file":"manifest.js","names":[],"sources":["../../src/manifest.ts"],"sourcesContent":["export type AssetCrossOrigin = 'anonymous' | 'use-credentials'\n\nexport type AssetCrossOriginConfig =\n | AssetCrossOrigin\n | Partial<Record<'modulepreload' | 'stylesheet', AssetCrossOrigin>>\n\nexport type ManifestAssetLink =\n | string\n | {\n href: string\n crossOrigin?: AssetCrossOrigin\n }\n\nexport function getAssetCrossOrigin(\n assetCrossOrigin: AssetCrossOriginConfig | undefined,\n kind: 'modulepreload' | 'stylesheet',\n): AssetCrossOrigin | undefined {\n if (!assetCrossOrigin) {\n return undefined\n }\n\n if (typeof assetCrossOrigin === 'string') {\n return assetCrossOrigin\n }\n\n return assetCrossOrigin[kind]\n}\n\nexport function resolveManifestAssetLink(link: ManifestAssetLink) {\n if (typeof link === 'string') {\n return { href: link, crossOrigin: undefined }\n }\n\n return link\n}\n\nexport type Manifest = {\n inlineCss?: {\n styles: Record<string, string>\n }\n routes: Record<\n string,\n {\n filePath?: string\n preloads?: Array<ManifestAssetLink>\n assets?: Array<RouterManagedTag>\n }\n >\n}\n\nexport type RouterManagedTag =\n | {\n tag: 'title'\n attrs?: Record<string, any>\n children: string\n }\n | {\n tag: 'meta' | 'link'\n attrs?: Record<string, any>\n children?: never\n }\n | {\n tag: 'script'\n attrs?: Record<string, any>\n children?: string\n }\n | {\n tag: 'style'\n attrs?: Record<string, any>\n children?: string\n inlineCss?: true\n }\n\nexport function getStylesheetHref(asset: RouterManagedTag) {\n if (asset.tag !== 'link') return undefined\n\n const rel = asset.attrs?.rel\n const href = asset.attrs?.href\n if (typeof href !== 'string') return undefined\n\n const relTokens = typeof rel === 'string' ? rel.split(/\\s+/) : []\n if (!relTokens.includes('stylesheet')) return undefined\n\n return href\n}\n\nexport function isInlinableStylesheet(\n manifest: Manifest | undefined,\n asset: RouterManagedTag,\n) {\n const href = getStylesheetHref(asset)\n return !!href && manifest?.inlineCss?.styles[href] !== undefined\n}\n\nexport function createInlineCssStyleAsset(css: string): RouterManagedTag {\n return {\n tag: 'style',\n attrs: {\n suppressHydrationWarning: true,\n },\n inlineCss: true,\n children: css,\n }\n}\n\nexport function createInlineCssPlaceholderAsset(): RouterManagedTag {\n return {\n tag: 'style',\n attrs: {\n suppressHydrationWarning: true,\n },\n inlineCss: true,\n }\n}\n"],"mappings":";AAaA,SAAgB,oBACd,kBACA,MAC8B;AAC9B,KAAI,CAAC,iBACH;AAGF,KAAI,OAAO,qBAAqB,SAC9B,QAAO;AAGT,QAAO,iBAAiB;;AAG1B,SAAgB,yBAAyB,MAAyB;AAChE,KAAI,OAAO,SAAS,SAClB,QAAO;EAAE,MAAM;EAAM,aAAa,KAAA;EAAW;AAG/C,QAAO;;AAwCT,SAAgB,kBAAkB,OAAyB;AACzD,KAAI,MAAM,QAAQ,OAAQ,QAAO,KAAA;CAEjC,MAAM,MAAM,MAAM,OAAO;CACzB,MAAM,OAAO,MAAM,OAAO;AAC1B,KAAI,OAAO,SAAS,SAAU,QAAO,KAAA;AAGrC,KAAI,EADc,OAAO,QAAQ,WAAW,IAAI,MAAM,MAAM,GAAG,EAAE,EAClD,SAAS,aAAa,CAAE,QAAO,KAAA;AAE9C,QAAO;;AAGT,SAAgB,sBACd,UACA,OACA;CACA,MAAM,OAAO,kBAAkB,MAAM;AACrC,QAAO,CAAC,CAAC,QAAQ,UAAU,WAAW,OAAO,UAAU,KAAA;;AAGzD,SAAgB,0BAA0B,KAA+B;AACvE,QAAO;EACL,KAAK;EACL,OAAO,EACL,0BAA0B,MAC3B;EACD,WAAW;EACX,UAAU;EACX;;AAGH,SAAgB,kCAAoD;AAClE,QAAO;EACL,KAAK;EACL,OAAO,EACL,0BAA0B,MAC3B;EACD,WAAW;EACZ"}
|
|
@@ -2,6 +2,7 @@ import { decodePath } from "../utils.js";
|
|
|
2
2
|
import { invariant } from "../invariant.js";
|
|
3
3
|
import { createLRUCache } from "../lru-cache.js";
|
|
4
4
|
import { rootRouteId } from "../root.js";
|
|
5
|
+
import { createInlineCssPlaceholderAsset, createInlineCssStyleAsset, getStylesheetHref, isInlinableStylesheet } from "../manifest.js";
|
|
5
6
|
import { GLOBAL_TSR, TSR_SCRIPT_BARRIER_ID } from "./constants.js";
|
|
6
7
|
import { makeSsrSerovalPlugin } from "./serializer/transformer.js";
|
|
7
8
|
import { defaultSerovalPlugins } from "./serializer/seroval-plugins.js";
|
|
@@ -95,6 +96,7 @@ var ScriptBuffer = class {
|
|
|
95
96
|
var isProd = process.env.NODE_ENV === "production";
|
|
96
97
|
var MANIFEST_CACHE_SIZE = 100;
|
|
97
98
|
var manifestCaches = /* @__PURE__ */ new WeakMap();
|
|
99
|
+
var inlineCssCaches = /* @__PURE__ */ new WeakMap();
|
|
98
100
|
function getManifestCache(manifest) {
|
|
99
101
|
const cache = manifestCaches.get(manifest);
|
|
100
102
|
if (cache) return cache;
|
|
@@ -102,17 +104,81 @@ function getManifestCache(manifest) {
|
|
|
102
104
|
manifestCaches.set(manifest, newCache);
|
|
103
105
|
return newCache;
|
|
104
106
|
}
|
|
107
|
+
function getInlineCssCache(manifest) {
|
|
108
|
+
const cache = inlineCssCaches.get(manifest);
|
|
109
|
+
if (cache) return cache;
|
|
110
|
+
const newCache = createLRUCache(MANIFEST_CACHE_SIZE);
|
|
111
|
+
inlineCssCaches.set(manifest, newCache);
|
|
112
|
+
return newCache;
|
|
113
|
+
}
|
|
114
|
+
function getInlineCssHrefsForMatches(manifest, matches) {
|
|
115
|
+
const styles = manifest?.inlineCss?.styles;
|
|
116
|
+
if (!styles) return [];
|
|
117
|
+
const seen = /* @__PURE__ */ new Set();
|
|
118
|
+
const hrefs = [];
|
|
119
|
+
for (const match of matches) {
|
|
120
|
+
const assets = manifest?.routes[match.routeId]?.assets ?? [];
|
|
121
|
+
for (const asset of assets) {
|
|
122
|
+
const href = getStylesheetHref(asset);
|
|
123
|
+
if (!href || seen.has(href) || styles[href] === void 0) continue;
|
|
124
|
+
seen.add(href);
|
|
125
|
+
hrefs.push(href);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return hrefs;
|
|
129
|
+
}
|
|
130
|
+
function getInlineCssForHrefs(manifest, hrefs) {
|
|
131
|
+
const styles = manifest.inlineCss?.styles;
|
|
132
|
+
if (!styles || hrefs.length === 0) return void 0;
|
|
133
|
+
const cacheKey = hrefs.join("\0");
|
|
134
|
+
if (isProd) {
|
|
135
|
+
const cached = getInlineCssCache(manifest).get(cacheKey);
|
|
136
|
+
if (cached !== void 0) return cached;
|
|
137
|
+
}
|
|
138
|
+
const css = hrefs.map((href) => styles[href]).join("");
|
|
139
|
+
if (isProd) getInlineCssCache(manifest).set(cacheKey, css);
|
|
140
|
+
return css;
|
|
141
|
+
}
|
|
142
|
+
function getInlineCssAssetForMatches(manifest, matches) {
|
|
143
|
+
if (!manifest?.inlineCss) return void 0;
|
|
144
|
+
const css = getInlineCssForHrefs(manifest, getInlineCssHrefsForMatches(manifest, matches));
|
|
145
|
+
return css === void 0 ? void 0 : createInlineCssStyleAsset(css);
|
|
146
|
+
}
|
|
147
|
+
function stripInlinedStylesheetAssets(manifest, routes, matches) {
|
|
148
|
+
if (!manifest.inlineCss) return routes;
|
|
149
|
+
const nextRoutes = {};
|
|
150
|
+
for (const [routeId, route] of Object.entries(routes)) {
|
|
151
|
+
const assets = route.assets?.filter((asset) => !isInlinableStylesheet(manifest, asset));
|
|
152
|
+
const nextRoute = { ...route };
|
|
153
|
+
if (assets) if (assets.length > 0) nextRoute.assets = assets;
|
|
154
|
+
else delete nextRoute.assets;
|
|
155
|
+
nextRoutes[routeId] = nextRoute;
|
|
156
|
+
}
|
|
157
|
+
if (getInlineCssAssetForMatches(manifest, matches)) {
|
|
158
|
+
const rootRoute = nextRoutes["__root__"] ?? {};
|
|
159
|
+
nextRoutes[rootRouteId] = {
|
|
160
|
+
...rootRoute,
|
|
161
|
+
assets: [createInlineCssPlaceholderAsset(), ...rootRoute.assets ?? []]
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
return nextRoutes;
|
|
165
|
+
}
|
|
105
166
|
function attachRouterServerSsrUtils({ router, manifest, getRequestAssets, includeUnmatchedRouteAssets = true }) {
|
|
106
167
|
router.ssr = { get manifest() {
|
|
107
168
|
const requestAssets = getRequestAssets?.();
|
|
108
|
-
|
|
169
|
+
const inlineCssAsset = getInlineCssAssetForMatches(manifest, router.stores.matches.get());
|
|
170
|
+
if (!requestAssets?.length && !inlineCssAsset) return manifest;
|
|
109
171
|
return {
|
|
110
172
|
...manifest,
|
|
111
173
|
routes: {
|
|
112
174
|
...manifest?.routes,
|
|
113
175
|
[rootRouteId]: {
|
|
114
176
|
...manifest?.routes?.[rootRouteId],
|
|
115
|
-
assets: [
|
|
177
|
+
assets: [
|
|
178
|
+
...requestAssets ?? [],
|
|
179
|
+
...inlineCssAsset ? [inlineCssAsset] : [],
|
|
180
|
+
...manifest?.routes?.["__root__"]?.assets ?? []
|
|
181
|
+
]
|
|
116
182
|
}
|
|
117
183
|
}
|
|
118
184
|
};
|
|
@@ -156,10 +222,10 @@ function attachRouterServerSsrUtils({ router, manifest, getRequestAssets, includ
|
|
|
156
222
|
if (currentRouteIds.has(routeId)) nextFilteredRoutes[routeId] = routeManifest;
|
|
157
223
|
else if (includeUnmatchedRouteAssets && routeManifest.assets && routeManifest.assets.length > 0) nextFilteredRoutes[routeId] = { assets: routeManifest.assets };
|
|
158
224
|
}
|
|
159
|
-
|
|
160
|
-
|
|
225
|
+
filteredRoutes = stripInlinedStylesheetAssets(manifest, nextFilteredRoutes, matchesToDehydrate);
|
|
226
|
+
if (isProd) getManifestCache(manifest).set(manifestCacheKey, filteredRoutes);
|
|
161
227
|
}
|
|
162
|
-
manifestToDehydrate = { routes: filteredRoutes };
|
|
228
|
+
manifestToDehydrate = { routes: { ...filteredRoutes } };
|
|
163
229
|
if (opts?.requestAssets?.length) {
|
|
164
230
|
const existingRoot = manifestToDehydrate.routes[rootRouteId];
|
|
165
231
|
manifestToDehydrate.routes[rootRouteId] = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ssr-server.js","names":[],"sources":["../../../src/ssr/ssr-server.ts"],"sourcesContent":["import { crossSerializeStream, getCrossReferenceHeader } from 'seroval'\nimport { invariant } from '../invariant'\nimport { decodePath } from '../utils'\nimport { createLRUCache } from '../lru-cache'\nimport { rootRouteId } from '../root'\nimport minifiedTsrBootStrapScript from './tsrScript?script-string'\nimport { GLOBAL_TSR, TSR_SCRIPT_BARRIER_ID } from './constants'\nimport { dehydrateSsrMatchId } from './ssr-match-id'\nimport { defaultSerovalPlugins } from './serializer/seroval-plugins'\nimport { makeSsrSerovalPlugin } from './serializer/transformer'\nimport type { LRUCache } from '../lru-cache'\nimport type { DehydratedMatch, DehydratedRouter } from './types'\nimport type { AnySerializationAdapter } from './serializer/transformer'\nimport type { AnyRouter } from '../router'\nimport type { AnyRouteMatch } from '../Matches'\nimport type { Manifest, RouterManagedTag } from '../manifest'\n\ndeclare module '../router' {\n interface ServerSsr {\n setRenderFinished: () => void\n cleanup: () => void\n }\n interface RouterEvents {\n onInjectedHtml: {\n type: 'onInjectedHtml'\n }\n onSerializationFinished: {\n type: 'onSerializationFinished'\n }\n }\n}\n\nconst SCOPE_ID = 'tsr'\n\nconst TSR_PREFIX = GLOBAL_TSR + '.router='\nconst P_PREFIX = GLOBAL_TSR + '.p(()=>'\nconst P_SUFFIX = ')'\n\nexport function dehydrateMatch(match: AnyRouteMatch): DehydratedMatch {\n const dehydratedMatch: DehydratedMatch = {\n i: dehydrateSsrMatchId(match.id),\n u: match.updatedAt,\n s: match.status,\n }\n\n const properties = [\n ['__beforeLoadContext', 'b'],\n ['loaderData', 'l'],\n ['error', 'e'],\n ['ssr', 'ssr'],\n ] as const\n\n for (const [key, shorthand] of properties) {\n if (match[key] !== undefined) {\n dehydratedMatch[shorthand] = match[key]\n }\n }\n if (match.globalNotFound) {\n dehydratedMatch.g = true\n }\n return dehydratedMatch\n}\n\nconst INITIAL_SCRIPTS = [\n getCrossReferenceHeader(SCOPE_ID),\n minifiedTsrBootStrapScript,\n]\n\nclass ScriptBuffer {\n private router: AnyRouter | undefined\n private _queue: Array<string>\n private _scriptBarrierLifted = false\n private _cleanedUp = false\n private _pendingMicrotask = false\n\n constructor(router: AnyRouter) {\n this.router = router\n // Copy INITIAL_SCRIPTS to avoid mutating the shared array\n this._queue = INITIAL_SCRIPTS.slice()\n }\n\n enqueue(script: string) {\n if (this._cleanedUp) return\n this._queue.push(script)\n // If barrier is lifted, schedule injection (if not already scheduled)\n if (this._scriptBarrierLifted && !this._pendingMicrotask) {\n this._pendingMicrotask = true\n queueMicrotask(() => {\n this._pendingMicrotask = false\n this.injectBufferedScripts()\n })\n }\n }\n\n liftBarrier() {\n if (this._scriptBarrierLifted || this._cleanedUp) return\n this._scriptBarrierLifted = true\n if (this._queue.length > 0 && !this._pendingMicrotask) {\n this._pendingMicrotask = true\n queueMicrotask(() => {\n this._pendingMicrotask = false\n this.injectBufferedScripts()\n })\n }\n }\n\n /**\n * Flushes any pending scripts synchronously.\n * Call this before emitting onSerializationFinished to ensure all scripts are injected.\n *\n * IMPORTANT: Only injects if the barrier has been lifted. Before the barrier is lifted,\n * scripts should remain in the queue so takeBufferedScripts() can retrieve them\n */\n flush() {\n if (!this._scriptBarrierLifted) return\n if (this._cleanedUp) return\n this._pendingMicrotask = false\n const scriptsToInject = this.takeAll()\n if (scriptsToInject && this.router?.serverSsr) {\n this.router.serverSsr.injectScript(scriptsToInject)\n }\n }\n\n takeAll() {\n const bufferedScripts = this._queue\n this._queue = []\n if (bufferedScripts.length === 0) {\n return undefined\n }\n // Optimization: if only one script, avoid join\n if (bufferedScripts.length === 1) {\n return bufferedScripts[0] + ';document.currentScript.remove()'\n }\n // Append cleanup script and join - avoid push() to not mutate then iterate\n return bufferedScripts.join(';') + ';document.currentScript.remove()'\n }\n\n injectBufferedScripts() {\n if (this._cleanedUp) return\n // Early return if queue is empty (avoids unnecessary takeAll() call)\n if (this._queue.length === 0) return\n const scriptsToInject = this.takeAll()\n if (scriptsToInject && this.router?.serverSsr) {\n this.router.serverSsr.injectScript(scriptsToInject)\n }\n }\n\n cleanup() {\n this._cleanedUp = true\n this._queue = []\n this.router = undefined\n }\n}\n\nconst isProd = process.env.NODE_ENV === 'production'\n\ntype FilteredRoutes = Manifest['routes']\n\ntype ManifestLRU = LRUCache<string, FilteredRoutes>\n\nconst MANIFEST_CACHE_SIZE = 100\nconst manifestCaches = new WeakMap<Manifest, ManifestLRU>()\n\nfunction getManifestCache(manifest: Manifest): ManifestLRU {\n const cache = manifestCaches.get(manifest)\n if (cache) return cache\n const newCache = createLRUCache<string, FilteredRoutes>(MANIFEST_CACHE_SIZE)\n manifestCaches.set(manifest, newCache)\n return newCache\n}\n\nexport function attachRouterServerSsrUtils({\n router,\n manifest,\n getRequestAssets,\n includeUnmatchedRouteAssets = true,\n}: {\n router: AnyRouter\n manifest: Manifest | undefined\n getRequestAssets?: () => Array<RouterManagedTag> | undefined\n includeUnmatchedRouteAssets?: boolean\n}) {\n router.ssr = {\n get manifest() {\n const requestAssets = getRequestAssets?.()\n if (!requestAssets?.length) return manifest\n // Merge request-scoped assets into root route without mutating cached manifest\n return {\n ...manifest,\n routes: {\n ...manifest?.routes,\n [rootRouteId]: {\n ...manifest?.routes?.[rootRouteId],\n assets: [\n ...requestAssets,\n ...(manifest?.routes?.[rootRouteId]?.assets ?? []),\n ],\n },\n },\n }\n },\n }\n let _dehydrated = false\n let _serializationFinished = false\n const renderFinishedListeners: Array<() => void> = []\n const serializationFinishedListeners: Array<() => void> = []\n const scriptBuffer = new ScriptBuffer(router)\n let injectedHtmlBuffer = ''\n\n router.serverSsr = {\n injectHtml: (html: string) => {\n if (!html) return\n // Buffer the HTML so it can be retrieved via takeBufferedHtml()\n injectedHtmlBuffer += html\n // Emit event to notify subscribers that new HTML is available\n router.emit({\n type: 'onInjectedHtml',\n })\n },\n injectScript: (script: string) => {\n if (!script) return\n const html = `<script${router.options.ssr?.nonce ? ` nonce='${router.options.ssr.nonce}'` : ''}>${script}</script>`\n router.serverSsr!.injectHtml(html)\n },\n dehydrate: async (opts?: { requestAssets?: Array<RouterManagedTag> }) => {\n if (_dehydrated) {\n if (process.env.NODE_ENV !== 'production') {\n throw new Error('Invariant failed: router is already dehydrated!')\n }\n\n invariant()\n }\n let matchesToDehydrate = router.stores.matches.get()\n if (router.isShell()) {\n // In SPA mode we only want to dehydrate the root match\n matchesToDehydrate = matchesToDehydrate.slice(0, 1)\n }\n const matches = matchesToDehydrate.map(dehydrateMatch)\n\n let manifestToDehydrate: Manifest | undefined = undefined\n // For currently matched routes, send full manifest (preloads + assets).\n // For unmatched routes, include assets only when includeUnmatchedRouteAssets\n // is true; otherwise omit them entirely. Preloads for unmatched routes are\n // still excluded because they are handled via dynamic imports.\n if (manifest) {\n // Prod-only caching; in dev manifests may be replaced/updated (HMR)\n const currentRouteIdsList = matchesToDehydrate.map((m) => m.routeId)\n const manifestCacheKey = `${currentRouteIdsList.join('\\0')}\\0includeUnmatchedRouteAssets=${includeUnmatchedRouteAssets}`\n\n let filteredRoutes: FilteredRoutes | undefined\n\n if (isProd) {\n filteredRoutes = getManifestCache(manifest).get(manifestCacheKey)\n }\n\n if (!filteredRoutes) {\n const currentRouteIds = new Set(currentRouteIdsList)\n const nextFilteredRoutes: FilteredRoutes = {}\n\n for (const routeId in manifest.routes) {\n const routeManifest = manifest.routes[routeId]!\n if (currentRouteIds.has(routeId)) {\n nextFilteredRoutes[routeId] = routeManifest\n } else if (\n includeUnmatchedRouteAssets &&\n routeManifest.assets &&\n routeManifest.assets.length > 0\n ) {\n nextFilteredRoutes[routeId] = {\n assets: routeManifest.assets,\n }\n }\n }\n\n if (isProd) {\n getManifestCache(manifest).set(manifestCacheKey, nextFilteredRoutes)\n }\n\n filteredRoutes = nextFilteredRoutes\n }\n\n manifestToDehydrate = {\n routes: filteredRoutes,\n }\n\n // Merge request-scoped assets into root route (without mutating cached manifest)\n if (opts?.requestAssets?.length) {\n const existingRoot = manifestToDehydrate.routes[rootRouteId]\n manifestToDehydrate.routes[rootRouteId] = {\n ...existingRoot,\n assets: [...opts.requestAssets, ...(existingRoot?.assets ?? [])],\n }\n }\n }\n const dehydratedRouter: DehydratedRouter = {\n manifest: manifestToDehydrate,\n matches,\n }\n const lastMatchId = matchesToDehydrate[matchesToDehydrate.length - 1]?.id\n if (lastMatchId) {\n dehydratedRouter.lastMatchId = dehydrateSsrMatchId(lastMatchId)\n }\n const dehydratedData = await router.options.dehydrate?.()\n if (dehydratedData) {\n dehydratedRouter.dehydratedData = dehydratedData\n }\n _dehydrated = true\n\n const trackPlugins = { didRun: false }\n const serializationAdapters = router.options.serializationAdapters as\n | Array<AnySerializationAdapter>\n | undefined\n const plugins = serializationAdapters\n ? serializationAdapters\n .map((t) => makeSsrSerovalPlugin(t, trackPlugins))\n .concat(defaultSerovalPlugins)\n : defaultSerovalPlugins\n\n const signalSerializationComplete = () => {\n _serializationFinished = true\n try {\n serializationFinishedListeners.forEach((l) => l())\n router.emit({ type: 'onSerializationFinished' })\n } catch (err) {\n console.error('Serialization listener error:', err)\n } finally {\n serializationFinishedListeners.length = 0\n renderFinishedListeners.length = 0\n }\n }\n\n crossSerializeStream(dehydratedRouter, {\n refs: new Map(),\n plugins,\n onSerialize: (data, initial) => {\n let serialized = initial ? TSR_PREFIX + data : data\n if (trackPlugins.didRun) {\n serialized = P_PREFIX + serialized + P_SUFFIX\n }\n scriptBuffer.enqueue(serialized)\n },\n onError: (err: unknown) => {\n console.error('Serialization error:', err)\n if (err && (err as any).stack) {\n console.error((err as any).stack)\n }\n signalSerializationComplete()\n },\n scopeId: SCOPE_ID,\n onDone: () => {\n scriptBuffer.enqueue(GLOBAL_TSR + '.e()')\n // Flush all pending scripts synchronously before signaling completion\n // This ensures all scripts are injected before onSerializationFinished is emitted\n scriptBuffer.flush()\n signalSerializationComplete()\n },\n })\n },\n isDehydrated() {\n return _dehydrated\n },\n isSerializationFinished() {\n return _serializationFinished\n },\n onRenderFinished: (listener) => renderFinishedListeners.push(listener),\n onSerializationFinished: (listener) =>\n serializationFinishedListeners.push(listener),\n setRenderFinished: () => {\n // Wrap in try-catch to ensure scriptBuffer.liftBarrier() is always called\n try {\n renderFinishedListeners.forEach((l) => l())\n } catch (err) {\n console.error('Error in render finished listener:', err)\n } finally {\n // Clear listeners after calling them to prevent memory leaks\n renderFinishedListeners.length = 0\n }\n scriptBuffer.liftBarrier()\n },\n takeBufferedScripts() {\n const scripts = scriptBuffer.takeAll()\n const serverBufferedScript: RouterManagedTag = {\n tag: 'script',\n attrs: {\n nonce: router.options.ssr?.nonce,\n className: '$tsr',\n id: TSR_SCRIPT_BARRIER_ID,\n },\n children: scripts,\n }\n return serverBufferedScript\n },\n liftScriptBarrier() {\n scriptBuffer.liftBarrier()\n },\n takeBufferedHtml() {\n if (!injectedHtmlBuffer) {\n return undefined\n }\n const buffered = injectedHtmlBuffer\n injectedHtmlBuffer = ''\n return buffered\n },\n cleanup() {\n // Guard against multiple cleanup calls\n if (!router.serverSsr) return\n renderFinishedListeners.length = 0\n serializationFinishedListeners.length = 0\n injectedHtmlBuffer = ''\n scriptBuffer.cleanup()\n router.serverSsr = undefined\n },\n }\n}\n\n/**\n * Get the origin for the request.\n *\n * SECURITY: We intentionally do NOT trust the Origin header for determining\n * the router's origin. The Origin header can be spoofed by attackers, which\n * could lead to SSRF-like vulnerabilities where redirects are constructed\n * using a malicious origin (CVE-2024-34351).\n *\n * Instead, we derive the origin from request.url, which is typically set by\n * the server infrastructure (not client-controlled headers).\n *\n * For applications behind proxies that need to trust forwarded headers,\n * use the router's `origin` option to explicitly configure a trusted origin.\n */\nexport function getOrigin(request: Request) {\n try {\n return new URL(request.url).origin\n } catch {}\n return 'http://localhost'\n}\n\n// server and browser can decode/encode characters differently in paths and search params.\n// Server generally strictly follows the WHATWG URL Standard, while browsers may differ for legacy reasons.\n// for example, in paths \"|\" is not encoded on the server but is encoded on chromium (and not on firefox) while \"대\" is encoded on both sides.\n// Another anomaly is that in Node new URLSearchParams and new URL also decode/encode characters differently.\n// new URLSearchParams() encodes \"|\" while new URL() does not, and in this instance\n// chromium treats search params differently than paths, i.e. \"|\" is not encoded in search params.\nexport function getNormalizedURL(url: string | URL, base?: string | URL) {\n // ensure backslashes are encoded correctly in the URL\n if (typeof url === 'string') url = url.replace('\\\\', '%5C')\n\n const rawUrl = new URL(url, base)\n const { path: decodedPathname, handledProtocolRelativeURL } = decodePath(\n rawUrl.pathname,\n )\n const searchParams = new URLSearchParams(rawUrl.search)\n const normalizedHref =\n decodedPathname +\n (searchParams.size > 0 ? '?' : '') +\n searchParams.toString() +\n rawUrl.hash\n\n return {\n url: new URL(normalizedHref, rawUrl.origin),\n handledProtocolRelativeURL,\n }\n}\n"],"mappings":";;;;;;;;;;;AAgCA,IAAM,WAAW;AAEjB,IAAM,aAAa,aAAa;AAChC,IAAM,WAAW,aAAa;AAC9B,IAAM,WAAW;AAEjB,SAAgB,eAAe,OAAuC;CACpE,MAAM,kBAAmC;EACvC,GAAG,oBAAoB,MAAM,GAAG;EAChC,GAAG,MAAM;EACT,GAAG,MAAM;EACV;AASD,MAAK,MAAM,CAAC,KAAK,c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aAChD;AAER,4BAAwB,SAAS;;AAEnC,gBAAa,aAAa;;EAE5B,sBAAsB;GACpB,MAAM,UAAU,aAAa,SAAS;AAUtC,UAT+C;IAC7C,KAAK;IACL,OAAO;KACL,OAAO,OAAO,QAAQ,KAAK;KAC3B,WAAW;KACX,IAAI;KACL;IACD,UAAU;IACX;;EAGH,oBAAoB;AAClB,gBAAa,aAAa;;EAE5B,mBAAmB;AACjB,OAAI,CAAC,mBACH;GAEF,MAAM,WAAW;AACjB,wBAAqB;AACrB,UAAO;;EAET,UAAU;AAER,OAAI,CAAC,OAAO,UAAW;AACvB,2BAAwB,SAAS;AACjC,kCAA+B,SAAS;AACxC,wBAAqB;AACrB,gBAAa,SAAS;AACtB,UAAO,YAAY,KAAA;;EAEtB;;;;;;;;;;;;;;;;AAiBH,SAAgB,UAAU,SAAkB;AAC1C,KAAI;AACF,SAAO,IAAI,IAAI,QAAQ,IAAI,CAAC;SACtB;AACR,QAAO;;AAST,SAAgB,iBAAiB,KAAmB,MAAqB;AAEvE,KAAI,OAAO,QAAQ,SAAU,OAAM,IAAI,QAAQ,MAAM,MAAM;CAE3D,MAAM,SAAS,IAAI,IAAI,KAAK,KAAK;CACjC,MAAM,EAAE,MAAM,iBAAiB,+BAA+B,WAC5D,OAAO,SACR;CACD,MAAM,eAAe,IAAI,gBAAgB,OAAO,OAAO;CACvD,MAAM,iBACJ,mBACC,aAAa,OAAO,IAAI,MAAM,MAC/B,aAAa,UAAU,GACvB,OAAO;AAET,QAAO;EACL,KAAK,IAAI,IAAI,gBAAgB,OAAO,OAAO;EAC3C;EACD"}
|
|
1
|
+
{"version":3,"file":"ssr-server.js","names":[],"sources":["../../../src/ssr/ssr-server.ts"],"sourcesContent":["import { crossSerializeStream, getCrossReferenceHeader } from 'seroval'\nimport { invariant } from '../invariant'\nimport {\n createInlineCssPlaceholderAsset,\n createInlineCssStyleAsset,\n getStylesheetHref,\n isInlinableStylesheet,\n} from '../manifest'\nimport { decodePath } from '../utils'\nimport { createLRUCache } from '../lru-cache'\nimport { rootRouteId } from '../root'\nimport minifiedTsrBootStrapScript from './tsrScript?script-string'\nimport { GLOBAL_TSR, TSR_SCRIPT_BARRIER_ID } from './constants'\nimport { dehydrateSsrMatchId } from './ssr-match-id'\nimport { defaultSerovalPlugins } from './serializer/seroval-plugins'\nimport { makeSsrSerovalPlugin } from './serializer/transformer'\nimport type { LRUCache } from '../lru-cache'\nimport type { DehydratedMatch, DehydratedRouter } from './types'\nimport type { AnySerializationAdapter } from './serializer/transformer'\nimport type { AnyRouter } from '../router'\nimport type { AnyRouteMatch } from '../Matches'\nimport type { Manifest, RouterManagedTag } from '../manifest'\n\ndeclare module '../router' {\n interface ServerSsr {\n setRenderFinished: () => void\n cleanup: () => void\n }\n interface RouterEvents {\n onInjectedHtml: {\n type: 'onInjectedHtml'\n }\n onSerializationFinished: {\n type: 'onSerializationFinished'\n }\n }\n}\n\nconst SCOPE_ID = 'tsr'\n\nconst TSR_PREFIX = GLOBAL_TSR + '.router='\nconst P_PREFIX = GLOBAL_TSR + '.p(()=>'\nconst P_SUFFIX = ')'\n\nexport function dehydrateMatch(match: AnyRouteMatch): DehydratedMatch {\n const dehydratedMatch: DehydratedMatch = {\n i: dehydrateSsrMatchId(match.id),\n u: match.updatedAt,\n s: match.status,\n }\n\n const properties = [\n ['__beforeLoadContext', 'b'],\n ['loaderData', 'l'],\n ['error', 'e'],\n ['ssr', 'ssr'],\n ] as const\n\n for (const [key, shorthand] of properties) {\n if (match[key] !== undefined) {\n dehydratedMatch[shorthand] = match[key]\n }\n }\n if (match.globalNotFound) {\n dehydratedMatch.g = true\n }\n return dehydratedMatch\n}\n\nconst INITIAL_SCRIPTS = [\n getCrossReferenceHeader(SCOPE_ID),\n minifiedTsrBootStrapScript,\n]\n\nclass ScriptBuffer {\n private router: AnyRouter | undefined\n private _queue: Array<string>\n private _scriptBarrierLifted = false\n private _cleanedUp = false\n private _pendingMicrotask = false\n\n constructor(router: AnyRouter) {\n this.router = router\n // Copy INITIAL_SCRIPTS to avoid mutating the shared array\n this._queue = INITIAL_SCRIPTS.slice()\n }\n\n enqueue(script: string) {\n if (this._cleanedUp) return\n this._queue.push(script)\n // If barrier is lifted, schedule injection (if not already scheduled)\n if (this._scriptBarrierLifted && !this._pendingMicrotask) {\n this._pendingMicrotask = true\n queueMicrotask(() => {\n this._pendingMicrotask = false\n this.injectBufferedScripts()\n })\n }\n }\n\n liftBarrier() {\n if (this._scriptBarrierLifted || this._cleanedUp) return\n this._scriptBarrierLifted = true\n if (this._queue.length > 0 && !this._pendingMicrotask) {\n this._pendingMicrotask = true\n queueMicrotask(() => {\n this._pendingMicrotask = false\n this.injectBufferedScripts()\n })\n }\n }\n\n /**\n * Flushes any pending scripts synchronously.\n * Call this before emitting onSerializationFinished to ensure all scripts are injected.\n *\n * IMPORTANT: Only injects if the barrier has been lifted. Before the barrier is lifted,\n * scripts should remain in the queue so takeBufferedScripts() can retrieve them\n */\n flush() {\n if (!this._scriptBarrierLifted) return\n if (this._cleanedUp) return\n this._pendingMicrotask = false\n const scriptsToInject = this.takeAll()\n if (scriptsToInject && this.router?.serverSsr) {\n this.router.serverSsr.injectScript(scriptsToInject)\n }\n }\n\n takeAll() {\n const bufferedScripts = this._queue\n this._queue = []\n if (bufferedScripts.length === 0) {\n return undefined\n }\n // Optimization: if only one script, avoid join\n if (bufferedScripts.length === 1) {\n return bufferedScripts[0] + ';document.currentScript.remove()'\n }\n // Append cleanup script and join - avoid push() to not mutate then iterate\n return bufferedScripts.join(';') + ';document.currentScript.remove()'\n }\n\n injectBufferedScripts() {\n if (this._cleanedUp) return\n // Early return if queue is empty (avoids unnecessary takeAll() call)\n if (this._queue.length === 0) return\n const scriptsToInject = this.takeAll()\n if (scriptsToInject && this.router?.serverSsr) {\n this.router.serverSsr.injectScript(scriptsToInject)\n }\n }\n\n cleanup() {\n this._cleanedUp = true\n this._queue = []\n this.router = undefined\n }\n}\n\nconst isProd = process.env.NODE_ENV === 'production'\n\ntype FilteredRoutes = Manifest['routes']\n\ntype ManifestLRU = LRUCache<string, FilteredRoutes>\ntype InlineCssLRU = LRUCache<string, string>\n\nconst MANIFEST_CACHE_SIZE = 100\nconst manifestCaches = new WeakMap<Manifest, ManifestLRU>()\nconst inlineCssCaches = new WeakMap<Manifest, InlineCssLRU>()\n\nfunction getManifestCache(manifest: Manifest): ManifestLRU {\n const cache = manifestCaches.get(manifest)\n if (cache) return cache\n const newCache = createLRUCache<string, FilteredRoutes>(MANIFEST_CACHE_SIZE)\n manifestCaches.set(manifest, newCache)\n return newCache\n}\n\nfunction getInlineCssCache(manifest: Manifest): InlineCssLRU {\n const cache = inlineCssCaches.get(manifest)\n if (cache) return cache\n const newCache = createLRUCache<string, string>(MANIFEST_CACHE_SIZE)\n inlineCssCaches.set(manifest, newCache)\n return newCache\n}\n\nfunction getInlineCssHrefsForMatches(\n manifest: Manifest | undefined,\n matches: Array<AnyRouteMatch>,\n) {\n const styles = manifest?.inlineCss?.styles\n if (!styles) return []\n\n const seen = new Set<string>()\n const hrefs: Array<string> = []\n\n for (const match of matches) {\n const assets = manifest?.routes[match.routeId]?.assets ?? []\n for (const asset of assets) {\n const href = getStylesheetHref(asset)\n if (!href || seen.has(href) || styles[href] === undefined) {\n continue\n }\n seen.add(href)\n hrefs.push(href)\n }\n }\n\n return hrefs\n}\n\nfunction getInlineCssForHrefs(manifest: Manifest, hrefs: Array<string>) {\n const styles = manifest.inlineCss?.styles\n if (!styles || hrefs.length === 0) return undefined\n\n const cacheKey = hrefs.join('\\0')\n if (isProd) {\n const cached = getInlineCssCache(manifest).get(cacheKey)\n if (cached !== undefined) return cached\n }\n\n const css = hrefs.map((href) => styles[href]!).join('')\n\n if (isProd) {\n getInlineCssCache(manifest).set(cacheKey, css)\n }\n\n return css\n}\n\nfunction getInlineCssAssetForMatches(\n manifest: Manifest | undefined,\n matches: Array<AnyRouteMatch>,\n) {\n if (!manifest?.inlineCss) return undefined\n\n const hrefs = getInlineCssHrefsForMatches(manifest, matches)\n const css = getInlineCssForHrefs(manifest, hrefs)\n\n return css === undefined ? undefined : createInlineCssStyleAsset(css)\n}\n\nfunction stripInlinedStylesheetAssets(\n manifest: Manifest,\n routes: FilteredRoutes,\n matches: Array<AnyRouteMatch>,\n): FilteredRoutes {\n if (!manifest.inlineCss) {\n return routes\n }\n\n const nextRoutes: FilteredRoutes = {}\n\n for (const [routeId, route] of Object.entries(routes)) {\n const assets = route.assets?.filter(\n (asset) => !isInlinableStylesheet(manifest, asset),\n )\n\n const nextRoute = { ...route }\n if (assets) {\n if (assets.length > 0) {\n nextRoute.assets = assets\n } else {\n delete nextRoute.assets\n }\n }\n nextRoutes[routeId] = nextRoute\n }\n\n if (getInlineCssAssetForMatches(manifest, matches)) {\n const rootRoute = nextRoutes[rootRouteId] ?? {}\n nextRoutes[rootRouteId] = {\n ...rootRoute,\n assets: [createInlineCssPlaceholderAsset(), ...(rootRoute.assets ?? [])],\n }\n }\n\n return nextRoutes\n}\n\nexport function attachRouterServerSsrUtils({\n router,\n manifest,\n getRequestAssets,\n includeUnmatchedRouteAssets = true,\n}: {\n router: AnyRouter\n manifest: Manifest | undefined\n getRequestAssets?: () => Array<RouterManagedTag> | undefined\n includeUnmatchedRouteAssets?: boolean\n}) {\n router.ssr = {\n get manifest() {\n const requestAssets = getRequestAssets?.()\n const inlineCssAsset = getInlineCssAssetForMatches(\n manifest,\n router.stores.matches.get(),\n )\n if (!requestAssets?.length && !inlineCssAsset) return manifest\n // Merge request-scoped assets into root route without mutating cached manifest\n return {\n ...manifest,\n routes: {\n ...manifest?.routes,\n [rootRouteId]: {\n ...manifest?.routes?.[rootRouteId],\n assets: [\n ...(requestAssets ?? []),\n ...(inlineCssAsset ? [inlineCssAsset] : []),\n ...(manifest?.routes?.[rootRouteId]?.assets ?? []),\n ],\n },\n },\n }\n },\n }\n let _dehydrated = false\n let _serializationFinished = false\n const renderFinishedListeners: Array<() => void> = []\n const serializationFinishedListeners: Array<() => void> = []\n const scriptBuffer = new ScriptBuffer(router)\n let injectedHtmlBuffer = ''\n\n router.serverSsr = {\n injectHtml: (html: string) => {\n if (!html) return\n // Buffer the HTML so it can be retrieved via takeBufferedHtml()\n injectedHtmlBuffer += html\n // Emit event to notify subscribers that new HTML is available\n router.emit({\n type: 'onInjectedHtml',\n })\n },\n injectScript: (script: string) => {\n if (!script) return\n const html = `<script${router.options.ssr?.nonce ? ` nonce='${router.options.ssr.nonce}'` : ''}>${script}</script>`\n router.serverSsr!.injectHtml(html)\n },\n dehydrate: async (opts?: { requestAssets?: Array<RouterManagedTag> }) => {\n if (_dehydrated) {\n if (process.env.NODE_ENV !== 'production') {\n throw new Error('Invariant failed: router is already dehydrated!')\n }\n\n invariant()\n }\n let matchesToDehydrate = router.stores.matches.get()\n if (router.isShell()) {\n // In SPA mode we only want to dehydrate the root match\n matchesToDehydrate = matchesToDehydrate.slice(0, 1)\n }\n const matches = matchesToDehydrate.map(dehydrateMatch)\n\n let manifestToDehydrate: Manifest | undefined = undefined\n // For currently matched routes, send full manifest (preloads + assets).\n // For unmatched routes, include assets only when includeUnmatchedRouteAssets\n // is true; otherwise omit them entirely. Preloads for unmatched routes are\n // still excluded because they are handled via dynamic imports.\n if (manifest) {\n // Prod-only caching; in dev manifests may be replaced/updated (HMR)\n const currentRouteIdsList = matchesToDehydrate.map((m) => m.routeId)\n const manifestCacheKey = `${currentRouteIdsList.join('\\0')}\\0includeUnmatchedRouteAssets=${includeUnmatchedRouteAssets}`\n\n let filteredRoutes: FilteredRoutes | undefined\n\n if (isProd) {\n filteredRoutes = getManifestCache(manifest).get(manifestCacheKey)\n }\n\n if (!filteredRoutes) {\n const currentRouteIds = new Set(currentRouteIdsList)\n const nextFilteredRoutes: FilteredRoutes = {}\n\n for (const routeId in manifest.routes) {\n const routeManifest = manifest.routes[routeId]!\n if (currentRouteIds.has(routeId)) {\n nextFilteredRoutes[routeId] = routeManifest\n } else if (\n includeUnmatchedRouteAssets &&\n routeManifest.assets &&\n routeManifest.assets.length > 0\n ) {\n nextFilteredRoutes[routeId] = {\n assets: routeManifest.assets,\n }\n }\n }\n\n filteredRoutes = stripInlinedStylesheetAssets(\n manifest,\n nextFilteredRoutes,\n matchesToDehydrate,\n )\n\n if (isProd) {\n getManifestCache(manifest).set(manifestCacheKey, filteredRoutes)\n }\n }\n\n manifestToDehydrate = {\n routes: { ...filteredRoutes },\n }\n\n // Merge request-scoped assets into root route (without mutating cached manifest)\n if (opts?.requestAssets?.length) {\n const existingRoot = manifestToDehydrate.routes[rootRouteId]\n manifestToDehydrate.routes[rootRouteId] = {\n ...existingRoot,\n assets: [...opts.requestAssets, ...(existingRoot?.assets ?? [])],\n }\n }\n }\n const dehydratedRouter: DehydratedRouter = {\n manifest: manifestToDehydrate,\n matches,\n }\n const lastMatchId = matchesToDehydrate[matchesToDehydrate.length - 1]?.id\n if (lastMatchId) {\n dehydratedRouter.lastMatchId = dehydrateSsrMatchId(lastMatchId)\n }\n const dehydratedData = await router.options.dehydrate?.()\n if (dehydratedData) {\n dehydratedRouter.dehydratedData = dehydratedData\n }\n _dehydrated = true\n\n const trackPlugins = { didRun: false }\n const serializationAdapters = router.options.serializationAdapters as\n | Array<AnySerializationAdapter>\n | undefined\n const plugins = serializationAdapters\n ? serializationAdapters\n .map((t) => makeSsrSerovalPlugin(t, trackPlugins))\n .concat(defaultSerovalPlugins)\n : defaultSerovalPlugins\n\n const signalSerializationComplete = () => {\n _serializationFinished = true\n try {\n serializationFinishedListeners.forEach((l) => l())\n router.emit({ type: 'onSerializationFinished' })\n } catch (err) {\n console.error('Serialization listener error:', err)\n } finally {\n serializationFinishedListeners.length = 0\n renderFinishedListeners.length = 0\n }\n }\n\n crossSerializeStream(dehydratedRouter, {\n refs: new Map(),\n plugins,\n onSerialize: (data, initial) => {\n let serialized = initial ? TSR_PREFIX + data : data\n if (trackPlugins.didRun) {\n serialized = P_PREFIX + serialized + P_SUFFIX\n }\n scriptBuffer.enqueue(serialized)\n },\n onError: (err: unknown) => {\n console.error('Serialization error:', err)\n if (err && (err as any).stack) {\n console.error((err as any).stack)\n }\n signalSerializationComplete()\n },\n scopeId: SCOPE_ID,\n onDone: () => {\n scriptBuffer.enqueue(GLOBAL_TSR + '.e()')\n // Flush all pending scripts synchronously before signaling completion\n // This ensures all scripts are injected before onSerializationFinished is emitted\n scriptBuffer.flush()\n signalSerializationComplete()\n },\n })\n },\n isDehydrated() {\n return _dehydrated\n },\n isSerializationFinished() {\n return _serializationFinished\n },\n onRenderFinished: (listener) => renderFinishedListeners.push(listener),\n onSerializationFinished: (listener) =>\n serializationFinishedListeners.push(listener),\n setRenderFinished: () => {\n // Wrap in try-catch to ensure scriptBuffer.liftBarrier() is always called\n try {\n renderFinishedListeners.forEach((l) => l())\n } catch (err) {\n console.error('Error in render finished listener:', err)\n } finally {\n // Clear listeners after calling them to prevent memory leaks\n renderFinishedListeners.length = 0\n }\n scriptBuffer.liftBarrier()\n },\n takeBufferedScripts() {\n const scripts = scriptBuffer.takeAll()\n const serverBufferedScript: RouterManagedTag = {\n tag: 'script',\n attrs: {\n nonce: router.options.ssr?.nonce,\n className: '$tsr',\n id: TSR_SCRIPT_BARRIER_ID,\n },\n children: scripts,\n }\n return serverBufferedScript\n },\n liftScriptBarrier() {\n scriptBuffer.liftBarrier()\n },\n takeBufferedHtml() {\n if (!injectedHtmlBuffer) {\n return undefined\n }\n const buffered = injectedHtmlBuffer\n injectedHtmlBuffer = ''\n return buffered\n },\n cleanup() {\n // Guard against multiple cleanup calls\n if (!router.serverSsr) return\n renderFinishedListeners.length = 0\n serializationFinishedListeners.length = 0\n injectedHtmlBuffer = ''\n scriptBuffer.cleanup()\n router.serverSsr = undefined\n },\n }\n}\n\n/**\n * Get the origin for the request.\n *\n * SECURITY: We intentionally do NOT trust the Origin header for determining\n * the router's origin. The Origin header can be spoofed by attackers, which\n * could lead to SSRF-like vulnerabilities where redirects are constructed\n * using a malicious origin (CVE-2024-34351).\n *\n * Instead, we derive the origin from request.url, which is typically set by\n * the server infrastructure (not client-controlled headers).\n *\n * For applications behind proxies that need to trust forwarded headers,\n * use the router's `origin` option to explicitly configure a trusted origin.\n */\nexport function getOrigin(request: Request) {\n try {\n return new URL(request.url).origin\n } catch {}\n return 'http://localhost'\n}\n\n// server and browser can decode/encode characters differently in paths and search params.\n// Server generally strictly follows the WHATWG URL Standard, while browsers may differ for legacy reasons.\n// for example, in paths \"|\" is not encoded on the server but is encoded on chromium (and not on firefox) while \"대\" is encoded on both sides.\n// Another anomaly is that in Node new URLSearchParams and new URL also decode/encode characters differently.\n// new URLSearchParams() encodes \"|\" while new URL() does not, and in this instance\n// chromium treats search params differently than paths, i.e. \"|\" is not encoded in search params.\nexport function getNormalizedURL(url: string | URL, base?: string | URL) {\n // ensure backslashes are encoded correctly in the URL\n if (typeof url === 'string') url = url.replace('\\\\', '%5C')\n\n const rawUrl = new URL(url, base)\n const { path: decodedPathname, handledProtocolRelativeURL } = decodePath(\n rawUrl.pathname,\n )\n const searchParams = new URLSearchParams(rawUrl.search)\n const normalizedHref =\n decodedPathname +\n (searchParams.size > 0 ? '?' : '') +\n searchParams.toString() +\n rawUrl.hash\n\n return {\n url: new URL(normalizedHref, rawUrl.origin),\n handledProtocolRelativeURL,\n }\n}\n"],"mappings":";;;;;;;;;;;;AAsCA,IAAM,WAAW;AAEjB,IAAM,aAAa,aAAa;AAChC,IAAM,WAAW,aAAa;AAC9B,IAAM,WAAW;AAEjB,SAAgB,eAAe,OAAuC;CACpE,MAAM,kBAAmC;EACvC,GAAG,oBAAoB,MAAM,GAAG;EAChC,GAAG,MAAM;EACT,GAAG,MAAM;EACV;AASD,MAAK,MAAM,CAAC,KAAK,cAPE;EACjB,CAAC,uBAAuB,IAAI;EAC5B,CAAC,cAAc,IAAI;EACnB,CAAC,SAAS,IAAI;EACd,CAAC,OAAO,MAAM;EACf,CAGC,KAAI,MAAM,SAAS,KAAA,EACjB,iBAAgB,aAAa,MAAM;AAGvC,KAAI,MAAM,eACR,iBAAgB,IAAI;AAEtB,QAAO;;AAGT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IAAI,IAAI,KAAK,KAAK;CACjC,MAAM,EAAE,MAAM,iBAAiB,+BAA+B,WAC5D,OAAO,SACR;CACD,MAAM,eAAe,IAAI,gBAAgB,OAAO,OAAO;CACvD,MAAM,iBACJ,mBACC,aAAa,OAAO,IAAI,MAAM,MAC/B,aAAa,UAAU,GACvB,OAAO;AAET,QAAO;EACL,KAAK,IAAI,IAAI,gBAAgB,OAAO,OAAO;EAC3C;EACD"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -75,7 +75,13 @@ export type {
|
|
|
75
75
|
AssetCrossOriginConfig,
|
|
76
76
|
ManifestAssetLink,
|
|
77
77
|
} from './manifest'
|
|
78
|
-
export {
|
|
78
|
+
export {
|
|
79
|
+
createInlineCssStyleAsset,
|
|
80
|
+
getAssetCrossOrigin,
|
|
81
|
+
getStylesheetHref,
|
|
82
|
+
isInlinableStylesheet,
|
|
83
|
+
resolveManifestAssetLink,
|
|
84
|
+
} from './manifest'
|
|
79
85
|
export { isMatch } from './Matches'
|
|
80
86
|
export type {
|
|
81
87
|
AnyMatchAndValue,
|
package/src/manifest.ts
CHANGED
|
@@ -35,6 +35,9 @@ export function resolveManifestAssetLink(link: ManifestAssetLink) {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export type Manifest = {
|
|
38
|
+
inlineCss?: {
|
|
39
|
+
styles: Record<string, string>
|
|
40
|
+
}
|
|
38
41
|
routes: Record<
|
|
39
42
|
string,
|
|
40
43
|
{
|
|
@@ -65,4 +68,47 @@ export type RouterManagedTag =
|
|
|
65
68
|
tag: 'style'
|
|
66
69
|
attrs?: Record<string, any>
|
|
67
70
|
children?: string
|
|
71
|
+
inlineCss?: true
|
|
68
72
|
}
|
|
73
|
+
|
|
74
|
+
export function getStylesheetHref(asset: RouterManagedTag) {
|
|
75
|
+
if (asset.tag !== 'link') return undefined
|
|
76
|
+
|
|
77
|
+
const rel = asset.attrs?.rel
|
|
78
|
+
const href = asset.attrs?.href
|
|
79
|
+
if (typeof href !== 'string') return undefined
|
|
80
|
+
|
|
81
|
+
const relTokens = typeof rel === 'string' ? rel.split(/\s+/) : []
|
|
82
|
+
if (!relTokens.includes('stylesheet')) return undefined
|
|
83
|
+
|
|
84
|
+
return href
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function isInlinableStylesheet(
|
|
88
|
+
manifest: Manifest | undefined,
|
|
89
|
+
asset: RouterManagedTag,
|
|
90
|
+
) {
|
|
91
|
+
const href = getStylesheetHref(asset)
|
|
92
|
+
return !!href && manifest?.inlineCss?.styles[href] !== undefined
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function createInlineCssStyleAsset(css: string): RouterManagedTag {
|
|
96
|
+
return {
|
|
97
|
+
tag: 'style',
|
|
98
|
+
attrs: {
|
|
99
|
+
suppressHydrationWarning: true,
|
|
100
|
+
},
|
|
101
|
+
inlineCss: true,
|
|
102
|
+
children: css,
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function createInlineCssPlaceholderAsset(): RouterManagedTag {
|
|
107
|
+
return {
|
|
108
|
+
tag: 'style',
|
|
109
|
+
attrs: {
|
|
110
|
+
suppressHydrationWarning: true,
|
|
111
|
+
},
|
|
112
|
+
inlineCss: true,
|
|
113
|
+
}
|
|
114
|
+
}
|
package/src/ssr/ssr-server.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { crossSerializeStream, getCrossReferenceHeader } from 'seroval'
|
|
2
2
|
import { invariant } from '../invariant'
|
|
3
|
+
import {
|
|
4
|
+
createInlineCssPlaceholderAsset,
|
|
5
|
+
createInlineCssStyleAsset,
|
|
6
|
+
getStylesheetHref,
|
|
7
|
+
isInlinableStylesheet,
|
|
8
|
+
} from '../manifest'
|
|
3
9
|
import { decodePath } from '../utils'
|
|
4
10
|
import { createLRUCache } from '../lru-cache'
|
|
5
11
|
import { rootRouteId } from '../root'
|
|
@@ -157,9 +163,11 @@ const isProd = process.env.NODE_ENV === 'production'
|
|
|
157
163
|
type FilteredRoutes = Manifest['routes']
|
|
158
164
|
|
|
159
165
|
type ManifestLRU = LRUCache<string, FilteredRoutes>
|
|
166
|
+
type InlineCssLRU = LRUCache<string, string>
|
|
160
167
|
|
|
161
168
|
const MANIFEST_CACHE_SIZE = 100
|
|
162
169
|
const manifestCaches = new WeakMap<Manifest, ManifestLRU>()
|
|
170
|
+
const inlineCssCaches = new WeakMap<Manifest, InlineCssLRU>()
|
|
163
171
|
|
|
164
172
|
function getManifestCache(manifest: Manifest): ManifestLRU {
|
|
165
173
|
const cache = manifestCaches.get(manifest)
|
|
@@ -169,6 +177,108 @@ function getManifestCache(manifest: Manifest): ManifestLRU {
|
|
|
169
177
|
return newCache
|
|
170
178
|
}
|
|
171
179
|
|
|
180
|
+
function getInlineCssCache(manifest: Manifest): InlineCssLRU {
|
|
181
|
+
const cache = inlineCssCaches.get(manifest)
|
|
182
|
+
if (cache) return cache
|
|
183
|
+
const newCache = createLRUCache<string, string>(MANIFEST_CACHE_SIZE)
|
|
184
|
+
inlineCssCaches.set(manifest, newCache)
|
|
185
|
+
return newCache
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function getInlineCssHrefsForMatches(
|
|
189
|
+
manifest: Manifest | undefined,
|
|
190
|
+
matches: Array<AnyRouteMatch>,
|
|
191
|
+
) {
|
|
192
|
+
const styles = manifest?.inlineCss?.styles
|
|
193
|
+
if (!styles) return []
|
|
194
|
+
|
|
195
|
+
const seen = new Set<string>()
|
|
196
|
+
const hrefs: Array<string> = []
|
|
197
|
+
|
|
198
|
+
for (const match of matches) {
|
|
199
|
+
const assets = manifest?.routes[match.routeId]?.assets ?? []
|
|
200
|
+
for (const asset of assets) {
|
|
201
|
+
const href = getStylesheetHref(asset)
|
|
202
|
+
if (!href || seen.has(href) || styles[href] === undefined) {
|
|
203
|
+
continue
|
|
204
|
+
}
|
|
205
|
+
seen.add(href)
|
|
206
|
+
hrefs.push(href)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return hrefs
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function getInlineCssForHrefs(manifest: Manifest, hrefs: Array<string>) {
|
|
214
|
+
const styles = manifest.inlineCss?.styles
|
|
215
|
+
if (!styles || hrefs.length === 0) return undefined
|
|
216
|
+
|
|
217
|
+
const cacheKey = hrefs.join('\0')
|
|
218
|
+
if (isProd) {
|
|
219
|
+
const cached = getInlineCssCache(manifest).get(cacheKey)
|
|
220
|
+
if (cached !== undefined) return cached
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const css = hrefs.map((href) => styles[href]!).join('')
|
|
224
|
+
|
|
225
|
+
if (isProd) {
|
|
226
|
+
getInlineCssCache(manifest).set(cacheKey, css)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return css
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function getInlineCssAssetForMatches(
|
|
233
|
+
manifest: Manifest | undefined,
|
|
234
|
+
matches: Array<AnyRouteMatch>,
|
|
235
|
+
) {
|
|
236
|
+
if (!manifest?.inlineCss) return undefined
|
|
237
|
+
|
|
238
|
+
const hrefs = getInlineCssHrefsForMatches(manifest, matches)
|
|
239
|
+
const css = getInlineCssForHrefs(manifest, hrefs)
|
|
240
|
+
|
|
241
|
+
return css === undefined ? undefined : createInlineCssStyleAsset(css)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function stripInlinedStylesheetAssets(
|
|
245
|
+
manifest: Manifest,
|
|
246
|
+
routes: FilteredRoutes,
|
|
247
|
+
matches: Array<AnyRouteMatch>,
|
|
248
|
+
): FilteredRoutes {
|
|
249
|
+
if (!manifest.inlineCss) {
|
|
250
|
+
return routes
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const nextRoutes: FilteredRoutes = {}
|
|
254
|
+
|
|
255
|
+
for (const [routeId, route] of Object.entries(routes)) {
|
|
256
|
+
const assets = route.assets?.filter(
|
|
257
|
+
(asset) => !isInlinableStylesheet(manifest, asset),
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
const nextRoute = { ...route }
|
|
261
|
+
if (assets) {
|
|
262
|
+
if (assets.length > 0) {
|
|
263
|
+
nextRoute.assets = assets
|
|
264
|
+
} else {
|
|
265
|
+
delete nextRoute.assets
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
nextRoutes[routeId] = nextRoute
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (getInlineCssAssetForMatches(manifest, matches)) {
|
|
272
|
+
const rootRoute = nextRoutes[rootRouteId] ?? {}
|
|
273
|
+
nextRoutes[rootRouteId] = {
|
|
274
|
+
...rootRoute,
|
|
275
|
+
assets: [createInlineCssPlaceholderAsset(), ...(rootRoute.assets ?? [])],
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return nextRoutes
|
|
280
|
+
}
|
|
281
|
+
|
|
172
282
|
export function attachRouterServerSsrUtils({
|
|
173
283
|
router,
|
|
174
284
|
manifest,
|
|
@@ -183,7 +293,11 @@ export function attachRouterServerSsrUtils({
|
|
|
183
293
|
router.ssr = {
|
|
184
294
|
get manifest() {
|
|
185
295
|
const requestAssets = getRequestAssets?.()
|
|
186
|
-
|
|
296
|
+
const inlineCssAsset = getInlineCssAssetForMatches(
|
|
297
|
+
manifest,
|
|
298
|
+
router.stores.matches.get(),
|
|
299
|
+
)
|
|
300
|
+
if (!requestAssets?.length && !inlineCssAsset) return manifest
|
|
187
301
|
// Merge request-scoped assets into root route without mutating cached manifest
|
|
188
302
|
return {
|
|
189
303
|
...manifest,
|
|
@@ -192,7 +306,8 @@ export function attachRouterServerSsrUtils({
|
|
|
192
306
|
[rootRouteId]: {
|
|
193
307
|
...manifest?.routes?.[rootRouteId],
|
|
194
308
|
assets: [
|
|
195
|
-
...requestAssets,
|
|
309
|
+
...(requestAssets ?? []),
|
|
310
|
+
...(inlineCssAsset ? [inlineCssAsset] : []),
|
|
196
311
|
...(manifest?.routes?.[rootRouteId]?.assets ?? []),
|
|
197
312
|
],
|
|
198
313
|
},
|
|
@@ -272,15 +387,19 @@ export function attachRouterServerSsrUtils({
|
|
|
272
387
|
}
|
|
273
388
|
}
|
|
274
389
|
|
|
390
|
+
filteredRoutes = stripInlinedStylesheetAssets(
|
|
391
|
+
manifest,
|
|
392
|
+
nextFilteredRoutes,
|
|
393
|
+
matchesToDehydrate,
|
|
394
|
+
)
|
|
395
|
+
|
|
275
396
|
if (isProd) {
|
|
276
|
-
getManifestCache(manifest).set(manifestCacheKey,
|
|
397
|
+
getManifestCache(manifest).set(manifestCacheKey, filteredRoutes)
|
|
277
398
|
}
|
|
278
|
-
|
|
279
|
-
filteredRoutes = nextFilteredRoutes
|
|
280
399
|
}
|
|
281
400
|
|
|
282
401
|
manifestToDehydrate = {
|
|
283
|
-
routes: filteredRoutes,
|
|
402
|
+
routes: { ...filteredRoutes },
|
|
284
403
|
}
|
|
285
404
|
|
|
286
405
|
// Merge request-scoped assets into root route (without mutating cached manifest)
|