diff --git a/lib/page-loader.js b/lib/page-loader.js index 9c7c1dc2..99bb47a4 100644 --- a/lib/page-loader.js +++ b/lib/page-loader.js @@ -1,4 +1,4 @@ -/* global window, document, __NEXT_DATA__ */ +/* global window, document */ import EventEmitter from './EventEmitter' const webpackModule = module @@ -69,11 +69,7 @@ export default class PageLoader { loadScript (route) { route = this.normalizeRoute(route) - let scriptRoute = route - - if (__NEXT_DATA__.nextExport) { - scriptRoute = route === '/' ? '/index.js' : `${route}.js` - } + const scriptRoute = route === '/' ? '/index.js' : `${route}.js` const script = document.createElement('script') const url = `${this.assetPrefix}/_next/${encodeURIComponent(this.buildId)}/page${scriptRoute}` diff --git a/server/index.js b/server/index.js index df313a70..31d1c815 100644 --- a/server/index.js +++ b/server/index.js @@ -194,12 +194,12 @@ export default class Server { await this.serveStatic(req, res, p) }, - '/_next/:buildId/page/:path*': async (req, res, params) => { + '/_next/:buildId/page/:path*.js': async (req, res, params) => { const paths = params.path || [''] // URL is asks for ${page}.js (to support loading assets from static dirs) // But there's no .js in the actual page. // So, we need to remove .js to get the page name. - const page = `/${paths.join('/')}`.replace('.js', '') + const page = `/${paths.join('/')}` if (!this.handleBuildId(params.buildId, res)) { const error = new Error('INVALID_BUILD_ID') @@ -222,8 +222,11 @@ export default class Server { } } - const p = join(this.dir, this.dist, 'bundles', 'pages', paths.join('/')) - await this.serveStatic(req, res, p) + let p = join(this.dir, this.dist, 'bundles', 'pages', paths.join('/')) + if (!fs.existsSync(`${p}.js`)) { + p = join(p, 'index') // It's possible to have index.js in a subfolder + } + await this.serveStatic(req, res, `${p}.js`) }, // It's very important keep this route's param optional.