Skip to content

Commit 371f82e

Browse files
author
hlimas
committed
feat(rsc): add support for renderBuiltInUrl on assets metadata
1 parent df6a38e commit 371f82e

7 files changed

Lines changed: 226 additions & 46 deletions

File tree

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

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
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'
5-
import { expectNoReload, testNoJs, waitForHydration } from './helper'
4+
import {
5+
expectNoReload,
6+
loadRSCManifest,
7+
testNoJs,
8+
waitForHydration,
9+
} from './helper'
610
import path from 'node:path'
711
import os from 'node:os'
812

@@ -72,6 +76,58 @@ test.describe('build-base', () => {
7276
defineTest(f)
7377
})
7478

79+
test.describe('dev-render-builtin-url-runtime', () => {
80+
const f = useFixture({
81+
root: 'examples/basic',
82+
mode: 'dev',
83+
cliOptions: {
84+
env: {
85+
TEST_RENDER_BUILTIN_URL: 'runtime',
86+
},
87+
},
88+
})
89+
defineTest(f)
90+
})
91+
92+
test.describe('build-render-builtin-url-runtime', () => {
93+
const f = useFixture({
94+
root: 'examples/basic',
95+
mode: 'build',
96+
cliOptions: {
97+
env: {
98+
TEST_RENDER_BUILTIN_URL: 'runtime',
99+
},
100+
},
101+
})
102+
defineTest(f)
103+
})
104+
105+
test.describe('dev-render-builtin-url-string', () => {
106+
const f = useFixture({
107+
root: 'examples/basic',
108+
mode: 'dev',
109+
cliOptions: {
110+
env: {
111+
TEST_RENDER_BUILTIN_URL: 'string',
112+
},
113+
},
114+
})
115+
defineTest(f)
116+
})
117+
118+
test.describe('build-render-builtin-url-string', () => {
119+
const f = useFixture({
120+
root: 'examples/basic',
121+
mode: 'build',
122+
cliOptions: {
123+
env: {
124+
TEST_RENDER_BUILTIN_URL: 'string',
125+
},
126+
},
127+
})
128+
defineTest(f)
129+
})
130+
75131
test.describe('dev-react-compiler', () => {
76132
const f = useFixture({
77133
root: 'examples/basic',
@@ -233,12 +289,9 @@ function defineTest(f: Fixture) {
233289
.evaluateAll((elements) =>
234290
elements.map((el) => el.getAttribute('href')),
235291
)
236-
const manifest = JSON.parse(
237-
readFileSync(
238-
f.root + '/dist/ssr/__vite_rsc_assets_manifest.js',
239-
'utf-8',
240-
).slice('export default '.length),
241-
)
292+
293+
const manifest = await loadRSCManifest(f.root)
294+
242295
const hashString = (v: string) =>
243296
createHash('sha256').update(v).digest().toString('hex').slice(0, 12)
244297
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),
@@ -42,3 +43,16 @@ export async function expectNoReload(page: Page) {
4243
},
4344
}
4445
}
46+
47+
export async function loadRSCManifest(root: string) {
48+
// Use dynamic "data:" import instead of URL path imports so it is
49+
// not cached by the runtime.
50+
const manifestFileContent = readFileSync(
51+
root + '/dist/ssr/__vite_rsc_assets_manifest.js',
52+
'utf-8',
53+
)
54+
const manifest = (await import('data:text/javascript,' + manifestFileContent))
55+
.default
56+
57+
return manifest
58+
}

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/examples/basic/vite.config.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ if (!(globalThis as any).__debugHandlerRegisterd) {
1818
}
1919

2020
export default defineConfig({
21-
base: process.env.TEST_BASE ? '/custom-base/' : undefined,
21+
base: process.env.TEST_BASE
22+
? '/custom-base/'
23+
: process.env.TEST_RENDER_BUILTIN_URL
24+
? './'
25+
: undefined,
2226
clearScreen: false,
2327
plugins: [
2428
tailwindcss(),
@@ -157,6 +161,19 @@ export default { fetch: handler };
157161
'@vitejs/test-dep-server-in-client/client',
158162
],
159163
},
164+
experimental: process.env.TEST_RENDER_BUILTIN_URL
165+
? {
166+
renderBuiltUrl(filename) {
167+
if (process.env.TEST_RENDER_BUILTIN_URL === 'runtime') {
168+
return {
169+
runtime: `'/' + ${JSON.stringify(filename)}`,
170+
}
171+
} else {
172+
return '/' + filename
173+
}
174+
},
175+
}
176+
: undefined,
160177
}) as any
161178

162179
function vitePluginUseCache(): Plugin[] {

packages/plugin-rsc/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
},
4040
"dependencies": {
4141
"@mjackson/node-fetch-server": "^0.7.0",
42+
"devalue": "^5.1.1",
4243
"es-module-lexer": "^1.7.0",
4344
"estree-walker": "^3.0.3",
4445
"magic-string": "^0.30.17",

0 commit comments

Comments
 (0)