Skip to content

Commit

Permalink
feat: Add an option to turn off the SPA behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- committed Nov 19, 2021
1 parent 4ffc9a5 commit c096e3d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/playground/nospa/__tests__/nospa.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// note: tests should retrieve the element at the beginning of test and reuse it
// in later assertions to ensure CSS HMR doesn't reload the page

const assertNotIndexHtml = async (path: string) => {
await page.goto(viteTestUrl + path)
const html = await page.innerHTML('body')
expect(html).not.toContain(
`This file should only ever be served as /index.html`
)
}
test('/index.html is served', async () => {
await page.goto(viteTestUrl + '/index.html')
const html = await page.innerHTML('body')
expect(html).toContain(`This file should only ever be served as /index.html`)
})
test('/ is not served', async () => {
await assertNotIndexHtml('/')
})
test('/foo/ is not served', async () => {
await assertNotIndexHtml('/foo/')
})
test('/foo/index.html is not served', async () => {
await assertNotIndexHtml('/foo/index.html')
})
3 changes: 3 additions & 0 deletions packages/playground/nospa/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>Hello</p>

<p>This file should only ever be served as /index.html in serve mode</p>
1 change: 1 addition & 0 deletions packages/playground/nospa/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Intentionally empty as no setup is needed
11 changes: 11 additions & 0 deletions packages/playground/nospa/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "nospa",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"preview": "vite preview"
}
}
9 changes: 9 additions & 0 deletions packages/playground/nospa/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
build: {
outDir: './dist',
emptyOutDir: true
},
server: {
noSpa: true
}
}
6 changes: 5 additions & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ export interface ServerOptions extends CommonServerOptions {
* Origin for the generated asset URLs.
*/
origin?: string
/**
* Do not serve index.html for all URLs which are not resources
*/
noSpa?: boolean
}

export interface ResolvedServerOptions extends ServerOptions {
Expand Down Expand Up @@ -477,7 +481,7 @@ export async function createServer(
middlewares.use(serveStaticMiddleware(root, server))

// spa fallback
if (!middlewareMode || middlewareMode === 'html') {
if (!config.server.noSpa && (!middlewareMode || middlewareMode === 'html')) {
middlewares.use(spaFallbackMiddleware(root))
}

Expand Down

0 comments on commit c096e3d

Please sign in to comment.