Skip to content

Commit

Permalink
Extract absolutePagePath util
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed Apr 30, 2022
1 parent b81564f commit 9f519bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
21 changes: 10 additions & 11 deletions packages/next/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
isDynamicRoute,
} from '../../shared/lib/router/utils'
import Server, { WrappedBuildError } from '../next-server'
import { normalizePagePath } from '../page-path-utils'
import { absolutePathToPage, normalizePagePath } from '../page-path-utils'
import Router from '../router'
import { getPathMatch } from '../../shared/lib/router/utils/path-match'
import { hasBasePath, replaceBasePath } from '../router-utils'
Expand Down Expand Up @@ -242,10 +242,8 @@ export default class DevServer extends Server {

let resolved = false
return new Promise((resolve, reject) => {
const pagesDir = this.pagesDir

// Watchpack doesn't emit an event for an empty directory
fs.readdir(pagesDir!, (_, files) => {
fs.readdir(this.pagesDir, (_, files) => {
if (files?.length) {
return
}
Expand All @@ -257,7 +255,7 @@ export default class DevServer extends Server {
})

let wp = (this.webpackWatcher = new Watchpack())
wp.watch([], [pagesDir!], 0)
wp.watch([], [this.pagesDir], 0)

wp.on('aggregated', async () => {
const routedMiddleware = []
Expand All @@ -270,20 +268,21 @@ export default class DevServer extends Server {
continue
}

const pageName = absolutePathToPage(
this.pagesDir,
fileName,
this.nextConfig.pageExtensions
)

if (regexMiddleware.test(fileName)) {
routedMiddleware.push(
`/${relative(pagesDir!, fileName).replace(/\\+/g, '/')}`
`/${relative(this.pagesDir, fileName).replace(/\\+/g, '/')}`
.replace(/^\/+/g, '/')
.replace(regexMiddleware, '/')
)
continue
}

let pageName =
'/' + relative(pagesDir!, fileName).replace(/\\+/g, '/')
pageName = pageName.replace(regexPageExtension, '')
pageName = pageName.replace(/\/index$/, '') || '/'

invalidatePageRuntimeCache(fileName, safeTime)
const pageRuntimeConfig = await getPageRuntime(
fileName,
Expand Down
23 changes: 22 additions & 1 deletion packages/next/server/page-path-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isDynamicRoute } from '../shared/lib/router/utils'
import { join, posix } from '../shared/lib/isomorphic/path'
import { join, posix, relative } from '../shared/lib/isomorphic/path'
import { flatten } from '../shared/lib/flatten'

/**
Expand Down Expand Up @@ -100,3 +100,24 @@ export function getPagePaths(normalizedPagePath: string, extensions: string[]) {
})
)
}

/**
* Given the absolute path to the pages folder, an absolute file path for a
* page and the page extensions, this function will return the page path
* relative to the pages folder. It doesn't consider index tail. Example:
* - `/Users/rick/my-project/pages/foo/bar/baz.js` -> `/foo/bar/baz`
*
* @param pagesDir Absolute path to the pages folder.
* @param filepath Absolute path to the page.
* @param extensions Extensions allowed for the page.
*/
export function absolutePathToPage(
pagesDir: string,
pagePath: string,
extensions: string[]
) {
return removePagePathTail(
normalizePathSep(ensureLeadingSlash(relative(pagesDir, pagePath))),
extensions
)
}

0 comments on commit 9f519bc

Please sign in to comment.