Skip to content

Commit 98e75df

Browse files
author
hlimas
committed
feat(rsc): add support for renderBuiltInUrl on assets metadata
1 parent 758dc73 commit 98e75df

File tree

5 files changed

+257
-43
lines changed

5 files changed

+257
-43
lines changed

packages/plugin-rsc/e2e/basic.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { createHash } from 'node:crypto'
2-
import { readFileSync } from 'node:fs'
32
import { type Page, expect, test } from '@playwright/test'
43
import { type Fixture, setupIsolatedFixture, useFixture } from './fixture'
54
import {
65
expectNoPageError,
76
expectNoReload,
7+
loadRSCManifest,
88
testNoJs,
99
waitForHydration,
1010
} from './helper'
@@ -177,12 +177,9 @@ function defineTest(f: Fixture) {
177177
.evaluateAll((elements) =>
178178
elements.map((el) => el.getAttribute('href')),
179179
)
180-
const manifest = JSON.parse(
181-
readFileSync(
182-
f.root + '/dist/ssr/__vite_rsc_assets_manifest.js',
183-
'utf-8',
184-
).slice('export default '.length),
185-
)
180+
181+
const manifest = await loadRSCManifest(f.root)
182+
186183
const hashString = (v: string) =>
187184
createHash('sha256').update(v).digest().toString('hex').slice(0, 12)
188185
const deps =

packages/plugin-rsc/e2e/helper.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test, { type Page, expect } from '@playwright/test'
2+
import { readFileSync } from 'node:fs'
23

34
export const testNoJs = test.extend({
45
javaScriptEnabled: ({}, use) => use(false),
@@ -54,3 +55,16 @@ export function expectNoPageError(page: Page) {
5455
},
5556
}
5657
}
58+
59+
export async function loadRSCManifest(root: string) {
60+
// Use dynamic "data:" import instead of URL path imports so it is
61+
// not cached by the runtime.
62+
const manifestFileContent = readFileSync(
63+
root + '/dist/ssr/__vite_rsc_assets_manifest.js',
64+
'utf-8',
65+
)
66+
const manifest = (await import('data:text/javascript,' + manifestFileContent))
67+
.default
68+
69+
return manifest
70+
}

packages/plugin-rsc/e2e/react-router.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { createHash } from 'node:crypto'
22
import { expect, test } from '@playwright/test'
33
import { type Fixture, useFixture } from './fixture'
4-
import { expectNoReload, testNoJs, waitForHydration } from './helper'
5-
import { readFileSync } from 'node:fs'
4+
import {
5+
expectNoReload,
6+
loadRSCManifest,
7+
testNoJs,
8+
waitForHydration,
9+
} from './helper'
610

711
test.describe('dev-default', () => {
812
const f = useFixture({ root: 'examples/react-router', mode: 'dev' })
@@ -74,12 +78,9 @@ function defineTest(f: Fixture) {
7478
.evaluateAll((elements) =>
7579
elements.map((el) => el.getAttribute('href')),
7680
)
77-
const manifest = JSON.parse(
78-
readFileSync(
79-
f.root + '/dist/ssr/__vite_rsc_assets_manifest.js',
80-
'utf-8',
81-
).slice('export default '.length),
82-
)
81+
82+
const manifest = await loadRSCManifest(f.root)
83+
8384
const hashString = (v: string) =>
8485
createHash('sha256').update(v).digest().toString('hex').slice(0, 12)
8586
const deps =

packages/plugin-rsc/e2e/starter.test.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,112 @@ test.describe(() => {
245245
})
246246
})
247247

248+
test.describe(() => {
249+
const root = 'examples/e2e/temp/base'
250+
251+
test.beforeAll(async () => {
252+
await setupInlineFixture({
253+
src: 'examples/starter',
254+
dest: root,
255+
files: {
256+
'vite.config.ts': /* js */ `
257+
import rsc from '@vitejs/plugin-rsc'
258+
import react from '@vitejs/plugin-react'
259+
import { defineConfig } from 'vite'
260+
261+
export default defineConfig({
262+
plugins: [
263+
react(),
264+
rsc({
265+
entries: {
266+
client: './src/framework/entry.browser.tsx',
267+
ssr: './src/framework/entry.ssr.tsx',
268+
rsc: './src/framework/entry.rsc.tsx',
269+
}
270+
}),
271+
],
272+
experimental: {
273+
renderBuiltUrl(filename) {
274+
return {
275+
runtime: \`'/' + \${JSON.stringify(filename)}\`
276+
}
277+
}
278+
}
279+
})
280+
`,
281+
},
282+
})
283+
})
284+
285+
test.describe('dev-render-built-url-runtime', () => {
286+
const f = useFixture({ root, mode: 'dev' })
287+
defineTest({
288+
...f,
289+
url: (url) => new URL(url ?? './', f.url('./custom-base/')).href,
290+
})
291+
})
292+
293+
test.describe('build-render-built-url-runtime', () => {
294+
const f = useFixture({ root, mode: 'build' })
295+
defineTest({
296+
...f,
297+
url: (url) => new URL(url ?? './', f.url('./custom-base/')).href,
298+
})
299+
})
300+
})
301+
302+
test.describe(() => {
303+
const root = 'examples/e2e/temp/base'
304+
305+
test.beforeAll(async () => {
306+
await setupInlineFixture({
307+
src: 'examples/starter',
308+
dest: root,
309+
files: {
310+
'vite.config.ts': /* js */ `
311+
import rsc from '@vitejs/plugin-rsc'
312+
import react from '@vitejs/plugin-react'
313+
import { defineConfig } from 'vite'
314+
315+
export default defineConfig({
316+
plugins: [
317+
react(),
318+
rsc({
319+
entries: {
320+
client: './src/framework/entry.browser.tsx',
321+
ssr: './src/framework/entry.ssr.tsx',
322+
rsc: './src/framework/entry.rsc.tsx',
323+
}
324+
}),
325+
],
326+
experimental: {
327+
renderBuiltUrl(filename) {
328+
return '/' + filename;
329+
}
330+
}
331+
})
332+
`,
333+
},
334+
})
335+
})
336+
337+
test.describe('dev-render-built-url-string', () => {
338+
const f = useFixture({ root, mode: 'dev' })
339+
defineTest({
340+
...f,
341+
url: (url) => new URL(url ?? './', f.url('./custom-base/')).href,
342+
})
343+
})
344+
345+
test.describe('build-render-built-url-string', () => {
346+
const f = useFixture({ root, mode: 'build' })
347+
defineTest({
348+
...f,
349+
url: (url) => new URL(url ?? './', f.url('./custom-base/')).href,
350+
})
351+
})
352+
})
353+
248354
function defineTest(f: Fixture, variant?: 'no-ssr' | 'dev-production') {
249355
const waitForHydration: typeof waitForHydration_ = (page) =>
250356
waitForHydration_(page, variant === 'no-ssr' ? '#root' : 'body')

0 commit comments

Comments
 (0)