diff --git a/playground/index.html b/playground/index.html index 2756476e249f6c..16156e816fb366 100644 --- a/playground/index.html +++ b/playground/index.html @@ -7,6 +7,6 @@
- + diff --git a/src/node/server/serverPluginModuleRewrite.ts b/src/node/server/serverPluginModuleRewrite.ts index fe2984df54d688..bdcfb18a03faaf 100644 --- a/src/node/server/serverPluginModuleRewrite.ts +++ b/src/node/server/serverPluginModuleRewrite.ts @@ -104,8 +104,8 @@ export const moduleRewritePlugin: ServerPlugin = ({ } else if (html) { ctx.body = await rewriteIndex(html) rewriteCache.set(html, ctx.body) - return } + return } // we are doing the js rewrite after all other middlewares have finished; diff --git a/src/node/server/serverPluginServeStatic.ts b/src/node/server/serverPluginServeStatic.ts index c42cd63fbf449c..6d12140b199718 100644 --- a/src/node/server/serverPluginServeStatic.ts +++ b/src/node/server/serverPluginServeStatic.ts @@ -1,3 +1,4 @@ +import path from 'path' import { ServerPlugin } from '.' const send = require('koa-send') @@ -26,31 +27,28 @@ export const serveStaticPlugin: ServerPlugin = ({ return next() } - if (ctx.path.includes('.')) { - debug(`not redirecting ${ctx.url} (relative url)`) - return next() - } - - if (!ctx.headers || typeof ctx.headers.accept !== 'string') { + const accept = ctx.headers && ctx.headers.accept + if (typeof accept !== 'string') { debug(`not redirecting ${ctx.url} (no headers.accept)`) return next() } - if (ctx.headers.accept.includes('application/json')) { + if (accept.includes('application/json')) { debug(`not redirecting ${ctx.url} (json)`) return next() } - if ( - !( - ctx.headers.accept.includes('text/html') || - ctx.headers.accept.includes('*/*') - ) - ) { + if (!(accept.includes('text/html') || accept.includes('*/*'))) { debug(`not redirecting ${ctx.url} (not accepting html)`) return next() } + const ext = path.extname(ctx.path) + if (ext && !accept.includes('text/html')) { + debug(`not redirecting ${ctx.url} (has file extension)`) + return next() + } + debug(`redirecting ${ctx.url} to /index.html`) ctx.url = '/index.html' return next()