Permalink
Browse files

fix: Handle 404 errors in SPA

  • Loading branch information...
pi0 committed Sep 7, 2017
1 parent 4d5255e commit 9b8d3f11e7af1968e5e6151c055d2b3fe60d1303
Showing with 11 additions and 1 deletion.
  1. +11 −1 lib/core/renderer.js
View
@@ -196,6 +196,10 @@ export default class Renderer extends Tapable {
this.app.use(path, handler)
}
get publicPath () {
return isUrl(this.options.build.publicPath) ? Options.defaults.build.publicPath : this.options.build.publicPath
}
async setupMiddleware () {
// Apply setupMiddleware from modules first
await this.applyPluginsAsync('setupMiddleware', this.app)
@@ -236,7 +240,7 @@ export default class Renderer extends Tapable {
if (!this.options.dev) {
const distDir = resolve(this.options.buildDir, 'dist')
this.useMiddleware({
path: isUrl(this.options.build.publicPath) ? Options.defaults.build.publicPath : this.options.build.publicPath,
path: this.publicPath,
handler: serveStatic(distDir, {
index: false, // Don't serve index.html template
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 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 = {
HTML_ATTRS,
BODY_ATTRS,

0 comments on commit 9b8d3f1

Please sign in to comment.