@@ -29,16 +29,20 @@ afterEach(() => {
2929} ) ;
3030
3131describe ( "injectPregeneratedConcretePaths" , ( ) => {
32- it ( "replaces an earlier injection" , ( ) => {
33- writeFile ( "dist/server/index.js" , 'import { handler } from "vinext/server/fetch-handler";\n' ) ;
32+ it ( "updates the sidecar without rewriting the built entry or its sourcemap" , ( ) => {
33+ const entry = 'import { handler } from "vinext/server/fetch-handler";\n' ;
34+ const sourceMap = '{"version":3,"sources":["entry.ts"]}\n' ;
35+ writeFile ( "dist/server/index-a1b2.js" , entry ) ;
36+ writeFile ( "dist/server/index-a1b2.js.map" , sourceMap ) ;
3437 writeFile (
3538 "dist/server/vinext-prerender.json" ,
3639 JSON . stringify ( {
3740 buildId : "build-a" ,
3841 pregeneratedConcretePaths : [ [ "/blog/:slug" , [ "/blog/post-a" ] ] ] ,
3942 } ) ,
4043 ) ;
41- injectPregeneratedConcretePaths ( tmpDir ) ;
44+ const entryPath = path . join ( tmpDir , "dist/server/index-a1b2.js" ) ;
45+ injectPregeneratedConcretePaths ( tmpDir , entryPath ) ;
4246
4347 writeFile (
4448 "dist/server/vinext-prerender.json" ,
@@ -47,31 +51,31 @@ describe("injectPregeneratedConcretePaths", () => {
4751 pregeneratedConcretePaths : [ [ "/blog/:slug" , [ "/blog/post-b" ] ] ] ,
4852 } ) ,
4953 ) ;
50- injectPregeneratedConcretePaths ( tmpDir ) ;
54+ injectPregeneratedConcretePaths ( tmpDir , entryPath ) ;
5155
52- const output = fs . readFileSync ( path . join ( tmpDir , "dist/server/index.js" ) , "utf-8" ) ;
53- expect ( output ) . toContain ( "post-b" ) ;
54- expect ( output ) . not . toContain ( "post-a" ) ;
55- expect ( output ) . toContain ( 'import { handler } from "vinext/server/fetch-handler"' ) ;
56+ const runtimeTable = fs . readFileSync (
57+ path . join ( tmpDir , "dist/server" , PREGENERATED_CONCRETE_PATHS_MODULE ) ,
58+ "utf-8" ,
59+ ) ;
60+ expect ( runtimeTable ) . toContain ( "post-b" ) ;
61+ expect ( runtimeTable ) . not . toContain ( "post-a" ) ;
62+ expect ( fs . readFileSync ( entryPath , "utf-8" ) ) . toBe ( entry ) ;
63+ expect ( fs . readFileSync ( `${ entryPath } .map` , "utf-8" ) ) . toBe ( sourceMap ) ;
5664 } ) ;
5765
58- it ( "strips an earlier injection when the manifest is missing" , ( ) => {
59- writeFile (
60- "dist/server/index.js" ,
61- [
62- "/* __VINEXT_PREGENERATED_CONCRETE_PATHS_START__ */" ,
63- 'globalThis.__VINEXT_PREGENERATED_CONCRETE_PATHS = [["/blog/:slug",["/blog/post-a"]]];' ,
64- "/* __VINEXT_PREGENERATED_CONCRETE_PATHS_END__ */" ,
65- 'import { handler } from "vinext/server/fetch-handler";' ,
66- "" ,
67- ] . join ( "\n" ) ,
68- ) ;
66+ it ( "clears the sidecar when the manifest is missing" , ( ) => {
67+ const entry = 'import { handler } from "vinext/server/fetch-handler";\n' ;
68+ writeFile ( "dist/server/index.js" , entry ) ;
6969
7070 injectPregeneratedConcretePaths ( tmpDir ) ;
7171
72- const output = fs . readFileSync ( path . join ( tmpDir , "dist/server/index.js" ) , "utf-8" ) ;
73- expect ( output ) . not . toContain ( "__VINEXT_PREGENERATED_CONCRETE_PATHS" ) ;
74- expect ( output ) . toContain ( 'import { handler } from "vinext/server/fetch-handler"' ) ;
72+ expect (
73+ fs . readFileSync (
74+ path . join ( tmpDir , "dist/server" , PREGENERATED_CONCRETE_PATHS_MODULE ) ,
75+ "utf-8" ,
76+ ) ,
77+ ) . toBe ( "delete globalThis.__VINEXT_PREGENERATED_CONCRETE_PATHS;\n" ) ;
78+ expect ( fs . readFileSync ( path . join ( tmpDir , "dist/server/index.js" ) , "utf-8" ) ) . toBe ( entry ) ;
7579 } ) ;
7680
7781 it ( "uses the concrete-path table stored in the prerender manifest" , ( ) => {
@@ -89,7 +93,10 @@ describe("injectPregeneratedConcretePaths", () => {
8993
9094 injectPregeneratedConcretePaths ( tmpDir ) ;
9195
92- const output = fs . readFileSync ( path . join ( tmpDir , "dist/server/index.js" ) , "utf-8" ) ;
96+ const output = fs . readFileSync (
97+ path . join ( tmpDir , "dist/server" , PREGENERATED_CONCRETE_PATHS_MODULE ) ,
98+ "utf-8" ,
99+ ) ;
93100 const match = output . match ( / g l o b a l T h i s \. _ _ V I N E X T _ P R E G E N E R A T E D _ C O N C R E T E _ P A T H S = ( \[ .* ?\] ) ; / ) ;
94101 expect ( match ) . not . toBeNull ( ) ;
95102 expect ( JSON . parse ( match ! [ 1 ] ) ) . toEqual ( [ [ "/blog/:slug" , [ "/blog/post-a" ] ] ] ) ;
@@ -100,16 +107,6 @@ describe("injectPregeneratedConcretePaths", () => {
100107
101108 it ( "clears the current-process global when no concrete paths are available" , ( ) => {
102109 globalThis . __VINEXT_PREGENERATED_CONCRETE_PATHS = [ [ "/old/:slug" , [ "/old/post" ] ] ] ;
103- writeFile (
104- "dist/server/index.js" ,
105- [
106- "/* __VINEXT_PREGENERATED_CONCRETE_PATHS_START__ */" ,
107- 'globalThis.__VINEXT_PREGENERATED_CONCRETE_PATHS = [["/old/:slug",["/old/post"]]];' ,
108- "/* __VINEXT_PREGENERATED_CONCRETE_PATHS_END__ */" ,
109- 'export default { fetch() { return new Response("ok"); } };' ,
110- "" ,
111- ] . join ( "\n" ) ,
112- ) ;
113110
114111 injectPregeneratedConcretePaths ( tmpDir ) ;
115112
@@ -123,6 +120,7 @@ describe("injectPregeneratedConcretePaths", () => {
123120 writeFile (
124121 "dist/server/index.js" ,
125122 [
123+ `import "./${ PREGENERATED_CONCRETE_PATHS_MODULE } ";` ,
126124 `import { getRenderedConcreteUrlPathsForRoute, initPregeneratedPathsFromGlobals } from ${ JSON . stringify ( registryModuleUrl ) } ;` ,
127125 "initPregeneratedPathsFromGlobals();" ,
128126 'export const renderedPaths = [...(getRenderedConcreteUrlPathsForRoute("/blog/:slug") ?? [])];' ,
@@ -180,9 +178,10 @@ describe("injectPregeneratedConcretePaths", () => {
180178 const registryModuleUrl = pathToFileURL (
181179 path . resolve ( "packages/vinext/src/server/pregenerated-concrete-paths.ts" ) ,
182180 ) . href ;
183- const entryPath = path . join ( tmpDir , "dist/custom-rsc/application-entry.js" ) ;
181+ const rscServerDir = path . join ( tmpDir , "dist/custom-rsc" ) ;
182+ const entryPath = path . join ( rscServerDir , "entries/application-entry.js" ) ;
184183 writeFile (
185- "dist/custom-rsc/application-entry.js" ,
184+ "dist/custom-rsc/entries/ application-entry.js" ,
186185 [
187186 `import "./${ PREGENERATED_CONCRETE_PATHS_MODULE } ";` ,
188187 `import { getRenderedConcreteUrlPathsForRoute, initPregeneratedPathsFromGlobals } from ${ JSON . stringify ( registryModuleUrl ) } ;` ,
@@ -199,7 +198,9 @@ describe("injectPregeneratedConcretePaths", () => {
199198 } ) ,
200199 ) ;
201200
202- injectPregeneratedConcretePaths ( tmpDir , entryPath ) ;
201+ injectPregeneratedConcretePaths ( tmpDir , entryPath , path . join ( tmpDir , "dist/server" ) , [
202+ rscServerDir ,
203+ ] ) ;
203204
204205 expect (
205206 fs . existsSync ( path . join ( path . dirname ( entryPath ) , PREGENERATED_CONCRETE_PATHS_MODULE ) ) ,
@@ -210,31 +211,30 @@ describe("injectPregeneratedConcretePaths", () => {
210211 "utf-8" ,
211212 ) ,
212213 ) . toContain ( "/blog/post-a" ) ;
214+ expect (
215+ fs . readFileSync ( path . join ( rscServerDir , PREGENERATED_CONCRETE_PATHS_MODULE ) , "utf-8" ) ,
216+ ) . toContain ( "/blog/post-a" ) ;
213217 const applicationEntry : unknown = await import (
214218 `${ pathToFileURL ( entryPath ) . href } ?t=${ Date . now ( ) } `
215219 ) ;
216220 expect ( applicationEntry ) . toMatchObject ( { renderedPaths : [ "/blog/post-a" ] } ) ;
217221 } ) ;
218222
219- it ( "strips an earlier injection when the manifest is corrupt" , ( ) => {
223+ it ( "clears the sidecar without rewriting the entry when the manifest is corrupt" , ( ) => {
220224 const warnSpy = vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } ) ;
221- writeFile (
222- "dist/server/index.js" ,
223- [
224- "/* __VINEXT_PREGENERATED_CONCRETE_PATHS_START__ */" ,
225- 'globalThis.__VINEXT_PREGENERATED_CONCRETE_PATHS = [["/",["/"]]];' ,
226- "/* __VINEXT_PREGENERATED_CONCRETE_PATHS_END__ */" ,
227- 'export default { fetch() { return new Response("ok"); } };' ,
228- "" ,
229- ] . join ( "\n" ) ,
230- ) ;
225+ const entry = 'export default { fetch() { return new Response("ok"); } };\n' ;
226+ writeFile ( "dist/server/index.js" , entry ) ;
231227 writeFile ( "dist/server/vinext-prerender.json" , "{invalid json}" ) ;
232228
233229 injectPregeneratedConcretePaths ( tmpDir ) ;
234230
235- const output = fs . readFileSync ( path . join ( tmpDir , "dist/server/index.js" ) , "utf-8" ) ;
236- expect ( output ) . not . toContain ( "__VINEXT_PREGENERATED_CONCRETE_PATHS" ) ;
237- expect ( output ) . toContain ( 'export default { fetch() { return new Response("ok"); } }' ) ;
231+ expect (
232+ fs . readFileSync (
233+ path . join ( tmpDir , "dist/server" , PREGENERATED_CONCRETE_PATHS_MODULE ) ,
234+ "utf-8" ,
235+ ) ,
236+ ) . toBe ( "delete globalThis.__VINEXT_PREGENERATED_CONCRETE_PATHS;\n" ) ;
237+ expect ( fs . readFileSync ( path . join ( tmpDir , "dist/server/index.js" ) , "utf-8" ) ) . toBe ( entry ) ;
238238 expect ( warnSpy ) . toHaveBeenCalledWith (
239239 expect . stringContaining ( "[vinext] Failed to read prerender manifest" ) ,
240240 expect . any ( SyntaxError ) ,
0 commit comments