Skip to content

Commit 572cbc8

Browse files
Copilothi-ogawa
andcommitted
Add spaPlugin and async_hooks polyfill to fix page loading
Co-authored-by: hi-ogawa <[email protected]>
1 parent 29ad4e0 commit 572cbc8

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Browser polyfill for node:async_hooks
2+
export class AsyncLocalStorage {
3+
constructor() {
4+
this.store = undefined
5+
}
6+
7+
run(store, callback, ...args) {
8+
const prev = this.store
9+
this.store = store
10+
try {
11+
return callback(...args)
12+
} finally {
13+
this.store = prev
14+
}
15+
}
16+
17+
getStore() {
18+
return this.store
19+
}
20+
}

packages/plugin-rsc/examples/browser-mode2/vite.config.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { defineConfig, type Plugin } from 'vite'
22
import react from '@vitejs/plugin-react'
33
import rsc from '@vitejs/plugin-rsc'
4+
import fsp from 'node:fs/promises'
45

56
export default defineConfig({
67
plugins: [
8+
spaPlugin(),
79
react(),
810
rsc({
911
entries: {
@@ -12,8 +14,84 @@ export default defineConfig({
1214
}),
1315
rscBrowserMode2Plugin(),
1416
],
17+
define: {
18+
'process.env.NODE_ENV': JSON.stringify('development'),
19+
},
20+
environments: {
21+
rsc: {
22+
resolve: {
23+
noExternal: true,
24+
alias: {
25+
'node:async_hooks': '/src/framework/async-hooks-polyfill.js',
26+
},
27+
},
28+
optimizeDeps: {
29+
include: [
30+
'react',
31+
'react-dom',
32+
'react/jsx-runtime',
33+
'react/jsx-dev-runtime',
34+
'@vitejs/plugin-rsc/vendor/react-server-dom/server.edge',
35+
'@vitejs/plugin-rsc/vendor/react-server-dom/client.edge',
36+
],
37+
exclude: ['@vitejs/plugin-rsc'],
38+
esbuildOptions: {
39+
platform: 'browser',
40+
},
41+
},
42+
},
43+
},
1544
})
1645

46+
function spaPlugin(): Plugin[] {
47+
// serve index.html before rsc server
48+
return [
49+
{
50+
name: 'serve-spa',
51+
configureServer(server) {
52+
return () => {
53+
server.middlewares.use(async (req, res, next) => {
54+
try {
55+
if (req.headers.accept?.includes('text/html')) {
56+
const html = await fsp.readFile('index.html', 'utf-8')
57+
const transformed = await server.transformIndexHtml('/', html)
58+
res.setHeader('Content-type', 'text/html')
59+
res.setHeader('Vary', 'accept')
60+
res.end(transformed)
61+
return
62+
}
63+
} catch (error) {
64+
next(error)
65+
return
66+
}
67+
next()
68+
})
69+
}
70+
},
71+
configurePreviewServer(server) {
72+
return () => {
73+
server.middlewares.use(async (req, res, next) => {
74+
try {
75+
if (req.headers.accept?.includes('text/html')) {
76+
const html = await fsp.readFile(
77+
'dist/client/index.html',
78+
'utf-8',
79+
)
80+
res.end(html)
81+
return
82+
}
83+
} catch (error) {
84+
next(error)
85+
return
86+
}
87+
next()
88+
})
89+
}
90+
},
91+
},
92+
]
93+
}
94+
1795
function rscBrowserMode2Plugin(): Plugin[] {
1896
return [
1997
{

0 commit comments

Comments
 (0)