Skip to content

Commit 37eb5b3

Browse files
authoredMay 28, 2022
fix: ssr-manifest no base (#8371)
1 parent 104caf9 commit 37eb5b3

File tree

8 files changed

+43
-35
lines changed

8 files changed

+43
-35
lines changed
 

‎packages/vite/src/node/ssr/ssrManifestPlugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
5959
const chunk = bundle[filename] as OutputChunk | undefined
6060
if (chunk) {
6161
chunk.viteMetadata.importedCss.forEach((file) => {
62-
deps.push(`/${file}`)
62+
deps.push(join(config.base, file))
6363
})
6464
chunk.imports.forEach(addDeps)
6565
}

‎playground/ssr-vue/__tests__/serve.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export async function serve() {
1313
const { build } = await import('vite')
1414
// client build
1515
await build({
16+
base: '/test/',
1617
root: rootDir,
1718
logLevel: 'silent', // exceptions are logged by Vitest
1819
build: {
@@ -24,6 +25,7 @@ export async function serve() {
2425
})
2526
// server build
2627
await build({
28+
base: '/test/',
2729
root: rootDir,
2830
logLevel: 'silent',
2931
build: {

‎playground/ssr-vue/__tests__/ssr-vue.spec.ts

+27-26
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,48 @@ import {
1010
untilUpdated
1111
} from '~utils'
1212

13-
const url = `http://localhost:${port}`
13+
const url = `http://localhost:${port}/test/`
1414

1515
test('vuex can be import succeed by named import', async () => {
1616
// wait networkidle for dynamic optimize vuex
17-
await page.goto(url + '/store', { waitUntil: 'networkidle' })
17+
await page.goto(url + 'store', { waitUntil: 'networkidle' })
1818
expect(await page.textContent('h1')).toMatch('bar')
1919

2020
// raw http request
21-
const storeHtml = await (await fetch(url + '/store')).text()
21+
const storeHtml = await (await fetch(url + 'store')).text()
2222
expect(storeHtml).toMatch('bar')
2323
})
2424

2525
test('/about', async () => {
26-
await page.goto(url + '/about')
26+
await page.goto(url + 'about')
2727
expect(await page.textContent('h1')).toMatch('About')
2828
// should not have hydration mismatch
2929
browserLogs.forEach((msg) => {
3030
expect(msg).not.toMatch('mismatch')
3131
})
3232

3333
// fetch sub route
34-
const aboutHtml = await (await fetch(url + '/about')).text()
34+
const aboutHtml = await (await fetch(url + 'about')).text()
3535
expect(aboutHtml).toMatch('About')
3636
if (isBuild) {
3737
// assert correct preload directive generation for async chunks and CSS
3838
expect(aboutHtml).not.toMatch(
39-
/link rel="modulepreload".*?href="\/assets\/Home\.\w{8}\.js"/
39+
/link rel="modulepreload".*?href="\/test\/assets\/Home\.\w{8}\.js"/
4040
)
4141
expect(aboutHtml).not.toMatch(
42-
/link rel="stylesheet".*?href="\/assets\/Home\.\w{8}\.css"/
42+
/link rel="stylesheet".*?href="\/test\/assets\/Home\.\w{8}\.css"/
4343
)
4444
expect(aboutHtml).toMatch(
45-
/link rel="modulepreload".*?href="\/assets\/About\.\w{8}\.js"/
45+
/link rel="modulepreload".*?href="\/test\/assets\/About\.\w{8}\.js"/
4646
)
4747
expect(aboutHtml).toMatch(
48-
/link rel="stylesheet".*?href="\/assets\/About\.\w{8}\.css"/
48+
/link rel="stylesheet".*?href="\/test\/assets\/About\.\w{8}\.css"/
4949
)
5050
}
5151
})
5252

5353
test('/external', async () => {
54-
await page.goto(url + '/external')
54+
await page.goto(url + 'external')
5555
expect(await page.textContent('div')).toMatch(
5656
'Example external component content'
5757
)
@@ -61,18 +61,18 @@ test('/external', async () => {
6161
})
6262

6363
// fetch sub route
64-
const externalHtml = await (await fetch(url + '/external')).text()
64+
const externalHtml = await (await fetch(url + 'external')).text()
6565
expect(externalHtml).toMatch('Example external component content')
6666
if (isBuild) {
6767
// assert correct preload directive generation for async chunks and CSS
6868
expect(externalHtml).not.toMatch(
69-
/link rel="modulepreload".*?href="\/assets\/Home\.\w{8}\.js"/
69+
/link rel="modulepreload".*?href="\/test\/assets\/Home\.\w{8}\.js"/
7070
)
7171
expect(externalHtml).not.toMatch(
72-
/link rel="stylesheet".*?href="\/assets\/Home\.\w{8}\.css"/
72+
/link rel="stylesheet".*?href="\/test\/assets\/Home\.\w{8}\.css"/
7373
)
7474
expect(externalHtml).toMatch(
75-
/link rel="modulepreload".*?href="\/assets\/External\.\w{8}\.js"/
75+
/link rel="modulepreload".*?href="\/test\/assets\/External\.\w{8}\.js"/
7676
)
7777
}
7878
})
@@ -90,23 +90,23 @@ test('/', async () => {
9090
if (isBuild) {
9191
// assert correct preload directive generation for async chunks and CSS
9292
expect(html).toMatch(
93-
/link rel="modulepreload".*?href="\/assets\/Home\.\w{8}\.js"/
93+
/link rel="modulepreload".*?href="\/test\/assets\/Home\.\w{8}\.js"/
9494
)
9595
expect(html).toMatch(
96-
/link rel="stylesheet".*?href="\/assets\/Home\.\w{8}\.css"/
96+
/link rel="stylesheet".*?href="\/test\/assets\/Home\.\w{8}\.css"/
9797
)
9898
// JSX component preload registration
9999
expect(html).toMatch(
100-
/link rel="modulepreload".*?href="\/assets\/Foo\.\w{8}\.js"/
100+
/link rel="modulepreload".*?href="\/test\/assets\/Foo\.\w{8}\.js"/
101101
)
102102
expect(html).toMatch(
103-
/link rel="stylesheet".*?href="\/assets\/Foo\.\w{8}\.css"/
103+
/link rel="stylesheet".*?href="\/test\/assets\/Foo\.\w{8}\.css"/
104104
)
105105
expect(html).not.toMatch(
106-
/link rel="modulepreload".*?href="\/assets\/About\.\w{8}\.js"/
106+
/link rel="modulepreload".*?href="\/test\/assets\/About\.\w{8}\.js"/
107107
)
108108
expect(html).not.toMatch(
109-
/link rel="stylesheet".*?href="\/assets\/About\.\w{8}\.css"/
109+
/link rel="stylesheet".*?href="\/test\/assets\/About\.\w{8}\.css"/
110110
)
111111
}
112112
})
@@ -132,7 +132,7 @@ test('asset', async () => {
132132
})
133133
const img = await page.$('img')
134134
expect(await img.getAttribute('src')).toMatch(
135-
isBuild ? /\/assets\/logo\.\w{8}\.png/ : '/src/assets/logo.png'
135+
isBuild ? /\/test\/assets\/logo\.\w{8}\.png/ : '/src/assets/logo.png'
136136
)
137137
})
138138

@@ -166,13 +166,13 @@ test('hmr', async () => {
166166

167167
test('client navigation', async () => {
168168
await page.goto(url)
169-
await untilUpdated(() => page.textContent('a[href="/about"]'), 'About')
170-
await page.click('a[href="/about"]')
169+
await untilUpdated(() => page.textContent('a[href="/test/about"]'), 'About')
170+
await page.click('a[href="/test/about"]')
171171
await untilUpdated(() => page.textContent('h1'), 'About')
172172
editFile('src/pages/About.vue', (code) => code.replace('About', 'changed'))
173173
await untilUpdated(() => page.textContent('h1'), 'changed')
174-
await page.click('a[href="/"]')
175-
await untilUpdated(() => page.textContent('a[href="/"]'), 'Home')
174+
await page.click('a[href="/test/"]')
175+
await untilUpdated(() => page.textContent('a[href="/test/"]'), 'Home')
176176
})
177177

178178
test('import.meta.url', async () => {
@@ -183,7 +183,8 @@ test('import.meta.url', async () => {
183183
test.runIf(isBuild)('dynamic css file should be preloaded', async () => {
184184
await page.goto(url)
185185
const homeHtml = await (await fetch(url)).text()
186-
const re = /link rel="modulepreload".*?href="\/assets\/(Home\.\w{8}\.js)"/
186+
const re =
187+
/link rel="modulepreload".*?href="\/test\/assets\/(Home\.\w{8}\.js)"/
187188
const filename = re.exec(homeHtml)[1]
188189
const manifest = require(resolve(
189190
process.cwd(),

‎playground/ssr-vue/server.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ async function createServer(
2929
let vite
3030
if (!isProd) {
3131
vite = await require('vite').createServer({
32+
base: '/test/',
3233
root,
3334
logLevel: isTest ? 'error' : 'info',
3435
server: {
@@ -49,6 +50,7 @@ async function createServer(
4950
} else {
5051
app.use(require('compression')())
5152
app.use(
53+
'/test/',
5254
require('serve-static')(resolve('dist/client'), {
5355
index: false
5456
})
@@ -57,7 +59,7 @@ async function createServer(
5759

5860
app.use('*', async (req, res) => {
5961
try {
60-
const url = req.originalUrl
62+
const url = req.originalUrl.replace('/test/', '/')
6163

6264
let template, render
6365
if (!isProd) {
@@ -90,8 +92,8 @@ async function createServer(
9092

9193
if (!isTest) {
9294
createServer().then(({ app }) =>
93-
app.listen(5173, () => {
94-
console.log('http://localhost:5173')
95+
app.listen(6173, () => {
96+
console.log('http://localhost:6173')
9597
})
9698
)
9799
}

‎playground/ssr-vue/src/entry-server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { createApp } from './main'
1+
import { basename } from 'path'
22
import { renderToString } from 'vue/server-renderer'
3-
import path, { basename } from 'path'
3+
import { createApp } from './main'
44

55
export async function render(url, manifest) {
66
const { app, router } = createApp()

‎playground/ssr-vue/src/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import App from './App.vue'
21
import { createSSRApp } from 'vue'
2+
import App from './App.vue'
33
import { createRouter } from './router'
44

55
// SSR requires a fresh app instance per request, therefore we export a function

‎playground/ssr-vue/src/router.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
createMemoryHistory,
32
createRouter as _createRouter,
3+
createMemoryHistory,
44
createWebHistory
55
} from 'vue-router'
66

@@ -20,7 +20,9 @@ export function createRouter() {
2020
return _createRouter({
2121
// use appropriate history implementation for server/client
2222
// import.meta.env.SSR is injected by Vite.
23-
history: import.meta.env.SSR ? createMemoryHistory() : createWebHistory(),
23+
history: import.meta.env.SSR
24+
? createMemoryHistory('/test/')
25+
: createWebHistory('/test/'),
2426
routes
2527
})
2628
}

‎playground/ssr-vue/vite.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const nestedVirtualId = '\0' + nestedVirtualFile
99
* @type {import('vite').UserConfig}
1010
*/
1111
module.exports = {
12+
base: '/test/',
1213
plugins: [
1314
vuePlugin(),
1415
vueJsx(),

0 commit comments

Comments
 (0)
Please sign in to comment.