mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-17 17:57:13 +00:00
fix: Handle 404 errors in SPA
This commit is contained in:
parent
4d5255e805
commit
9b8d3f11e7
@ -196,6 +196,10 @@ export default class Renderer extends Tapable {
|
|||||||
this.app.use(path, handler)
|
this.app.use(path, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get publicPath () {
|
||||||
|
return isUrl(this.options.build.publicPath) ? Options.defaults.build.publicPath : this.options.build.publicPath
|
||||||
|
}
|
||||||
|
|
||||||
async setupMiddleware () {
|
async setupMiddleware () {
|
||||||
// Apply setupMiddleware from modules first
|
// Apply setupMiddleware from modules first
|
||||||
await this.applyPluginsAsync('setupMiddleware', this.app)
|
await this.applyPluginsAsync('setupMiddleware', this.app)
|
||||||
@ -236,7 +240,7 @@ export default class Renderer extends Tapable {
|
|||||||
if (!this.options.dev) {
|
if (!this.options.dev) {
|
||||||
const distDir = resolve(this.options.buildDir, 'dist')
|
const distDir = resolve(this.options.buildDir, 'dist')
|
||||||
this.useMiddleware({
|
this.useMiddleware({
|
||||||
path: isUrl(this.options.build.publicPath) ? Options.defaults.build.publicPath : this.options.build.publicPath,
|
path: this.publicPath,
|
||||||
handler: serveStatic(distDir, {
|
handler: serveStatic(distDir, {
|
||||||
index: false, // Don't serve index.html template
|
index: false, // Don't serve index.html template
|
||||||
maxAge: (this.options.dev ? 0 : '1y') // 1 year in production
|
maxAge: (this.options.dev ? 0 : '1y') // 1 year in production
|
||||||
@ -459,6 +463,12 @@ export default class Renderer extends Tapable {
|
|||||||
const { HTML_ATTRS, BODY_ATTRS, HEAD, resourceHints } = await this.metaRenderer.render(context)
|
const { HTML_ATTRS, BODY_ATTRS, HEAD, resourceHints } = await this.metaRenderer.render(context)
|
||||||
const APP = `<div id="__nuxt">${this.resources.loadingHTML}</div>`
|
const APP = `<div id="__nuxt">${this.resources.loadingHTML}</div>`
|
||||||
|
|
||||||
|
// Detect 404 errors
|
||||||
|
if (url.includes(this.options.build.publicPath) || url.includes('__webpack')) {
|
||||||
|
const err = { statusCode: 404, message: this.options.messages.error_404, name: 'ResourceNotFound' }
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
HTML_ATTRS,
|
HTML_ATTRS,
|
||||||
BODY_ATTRS,
|
BODY_ATTRS,
|
||||||
|
Loading…
Reference in New Issue
Block a user