From 9b8d3f11e7af1968e5e6151c055d2b3fe60d1303 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2017 17:06:56 +0430 Subject: [PATCH] fix: Handle 404 errors in SPA --- lib/core/renderer.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/core/renderer.js b/lib/core/renderer.js index b11f473248..9a8bed02ea 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -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 = `
${this.resources.loadingHTML}
` + // 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,