Skip to content

Commit

Permalink
Normalize trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot committed Feb 23, 2019
1 parent b146eb5 commit 126d3aa
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/next-server/server/normalize-page-path.ts
Expand Up @@ -8,6 +8,8 @@ export function normalizePagePath(page: string): string {
if (page[0] !== '/') {
page = `/${page}`
}
// Strip trailing slash
page = page.replace(/\/$/, '')
// Throw when using ../ etc in the pathname
const resolvedPage = posix.normalize(page)
if (page !== resolvedPage) {
Expand Down
3 changes: 2 additions & 1 deletion packages/next/pages/_document.js
Expand Up @@ -205,6 +205,7 @@ export class NextScript extends Component {
const { _devOnlyInvalidateCacheQueryString } = this.context

return dynamicImports.map((bundle) => {
console.log(bundle.file)
return <script
async
key={bundle.file}
Expand Down Expand Up @@ -283,5 +284,5 @@ function getPagePathname (page) {
return '/index.js'
}

return `${page}.js`
return `${page.replace(/\/$/, '')}.js`
}
3 changes: 1 addition & 2 deletions packages/next/server/on-demand-entry-handler.js
Expand Up @@ -240,7 +240,6 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
const bundleFile = pageUrl === '/' ? '/index.js' : `${pageUrl}.js`
const name = join('static', buildId, 'pages', bundleFile)
const absolutePagePath = pagePath.startsWith('next/dist/pages') ? require.resolve(pagePath) : join(pagesDir, pagePath)

await new Promise((resolve, reject) => {
const entryInfo = entries[page]

Expand Down Expand Up @@ -356,7 +355,7 @@ export function normalizePage (page) {
if (unixPagePath === '/index' || unixPagePath === '/') {
return '/'
}
return unixPagePath.replace(/\/index$/, '')
return unixPagePath.replace(/\/(?:index)?$/, '')
}

// Make sure only one invalidation happens at a time
Expand Down
4 changes: 4 additions & 0 deletions test/isolated/require-page.test.js
Expand Up @@ -39,6 +39,10 @@ describe('normalizePagePath', () => {
it('Should throw on /../../test.js', () => {
expect(() => normalizePagePath('/../../test.js')).toThrow()
})

it('Should turn /abc/ into /abc', () => {
expect(normalizePagePath('/abc/')).toBe(`${sep}abc`)
})
})

describe('getPagePath', () => {
Expand Down

0 comments on commit 126d3aa

Please sign in to comment.