Skip to content

Commit a9a7319

Browse files
authored
Refactor moveExportedPages to use getPagePath (#14247)
Updates to not build the path from scratch here and relies on `getPagePath` instead Closes: #14223
1 parent 335e9d4 commit a9a7319

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

packages/next/build/index.ts

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -741,19 +741,40 @@ export default async function build(dir: string, conf = null): Promise<void> {
741741
const serverBundle = getPagePath(page, distDir, isLikeServerless)
742742
await promises.unlink(serverBundle)
743743
}
744+
const serverOutputDir = path.join(
745+
distDir,
746+
isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
747+
)
744748

745749
const moveExportedPage = async (
750+
originPage: string,
746751
page: string,
747752
file: string,
748753
isSsg: boolean,
749754
ext: 'html' | 'json'
750755
) => {
751756
file = `${file}.${ext}`
752757
const orig = path.join(exportOptions.outdir, file)
753-
const relativeDest = (isLikeServerless
754-
? path.join('pages', file)
755-
: path.join('static', buildId, 'pages', file)
756-
).replace(/\\/g, '/')
758+
const pagePath = getPagePath(originPage, distDir, isLikeServerless)
759+
760+
const relativeDest = path
761+
.relative(
762+
serverOutputDir,
763+
path.join(
764+
path.join(
765+
pagePath,
766+
// strip leading / and then recurse number of nested dirs
767+
// to place from base folder
768+
originPage
769+
.substr(1)
770+
.split('/')
771+
.map(() => '..')
772+
.join('/')
773+
),
774+
file
775+
)
776+
)
777+
.replace(/\\/g, '/')
757778

758779
const dest = path.join(
759780
distDir,
@@ -762,19 +783,15 @@ export default async function build(dir: string, conf = null): Promise<void> {
762783
)
763784

764785
if (!isSsg) {
765-
if (page === '/.amp') {
766-
pagesManifest['/index.amp'] = relativeDest
767-
} else {
768-
pagesManifest[page] = relativeDest
769-
}
786+
pagesManifest[page] = relativeDest
770787
}
771788
await promises.mkdir(path.dirname(dest), { recursive: true })
772789
await promises.rename(orig, dest)
773790
}
774791

775792
// Only move /404 to /404 when there is no custom 404 as in that case we don't know about the 404 page
776793
if (!hasPages404 && useStatic404) {
777-
await moveExportedPage('/404', '/404', false, 'html')
794+
await moveExportedPage('/_error', '/404', '/404', false, 'html')
778795
}
779796

780797
for (const page of combinedPages) {
@@ -787,21 +804,22 @@ export default async function build(dir: string, conf = null): Promise<void> {
787804
// The dynamic version of SSG pages are only prerendered if the fallback
788805
// is enabled. Below, we handle the specific prerenders of these.
789806
if (!(isSsg && isDynamic && !isSsgFallback)) {
790-
await moveExportedPage(page, file, isSsg, 'html')
807+
await moveExportedPage(page, page, file, isSsg, 'html')
791808
}
792809

793810
if (hasAmp && (!isSsg || (isSsg && !isDynamic))) {
794-
await moveExportedPage(`${page}.amp`, `${file}.amp`, isSsg, 'html')
811+
const ampPage = `${file}.amp`
812+
await moveExportedPage(page, ampPage, ampPage, isSsg, 'html')
795813

796814
if (isSsg) {
797-
await moveExportedPage(`${page}.amp`, `${file}.amp`, isSsg, 'json')
815+
await moveExportedPage(page, ampPage, ampPage, isSsg, 'json')
798816
}
799817
}
800818

801819
if (isSsg) {
802820
// For a non-dynamic SSG page, we must copy its data file from export.
803821
if (!isDynamic) {
804-
await moveExportedPage(page, file, true, 'json')
822+
await moveExportedPage(page, page, file, true, 'json')
805823

806824
finalPrerenderRoutes[page] = {
807825
initialRevalidateSeconds:
@@ -816,22 +834,13 @@ export default async function build(dir: string, conf = null): Promise<void> {
816834
// `getStaticPaths` (additionalSsgPaths).
817835
const extraRoutes = additionalSsgPaths.get(page) || []
818836
for (const route of extraRoutes) {
819-
await moveExportedPage(route, route, true, 'html')
820-
await moveExportedPage(route, route, true, 'json')
837+
await moveExportedPage(page, route, route, true, 'html')
838+
await moveExportedPage(page, route, route, true, 'json')
821839

822840
if (hasAmp) {
823-
await moveExportedPage(
824-
`${route}.amp`,
825-
`${route}.amp`,
826-
true,
827-
'html'
828-
)
829-
await moveExportedPage(
830-
`${route}.amp`,
831-
`${route}.amp`,
832-
true,
833-
'json'
834-
)
841+
const ampPage = `${normalizePagePath(route)}.amp`
842+
await moveExportedPage(page, ampPage, ampPage, true, 'html')
843+
await moveExportedPage(page, ampPage, ampPage, true, 'json')
835844
}
836845

837846
finalPrerenderRoutes[route] = {

test/integration/amp-export-validation/test/index.test.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ describe('AMP Validation on Export', () => {
2525

2626
it('should have shown errors during build', async () => {
2727
expect(buildOutput).toMatch(
28-
/error.*The parent tag of tag 'IMG-I-AMPHTML-INTRINSIC-SIZER' is 'div', but it can only be 'i-amphtml-sizer-intrinsic'\./
29-
)
30-
expect(buildOutput).toMatch(
31-
/error.*The parent tag of tag 'IMG-I-AMPHTML-INTRINSIC-SIZER' is 'div', but it can only be 'i-amphtml-sizer-intrinsic'/
28+
/error.*The parent tag of tag 'img' is 'div', but it can only be 'i-amphtml-sizer-intrinsic'\./
3229
)
3330
})
3431

@@ -85,7 +82,7 @@ describe('AMP Validation on Export', () => {
8582
stderr: true,
8683
})
8784
expect(stdout).toMatch(
88-
/error.*The parent tag of tag 'IMG-I-AMPHTML-INTRINSIC-SIZER' is 'div', but it can only be 'i-amphtml-sizer-intrinsic'/
85+
/error.*The parent tag of tag 'img' is 'div', but it can only be 'i-amphtml-sizer-intrinsic'\./
8986
)
9087
await expect(access(join(outDir, 'dog.html'))).resolves.toBe(undefined)
9188
await expect(stderr).not.toMatch(
@@ -112,10 +109,7 @@ describe('AMP Validation on Export', () => {
112109
stderr: true,
113110
})
114111
expect(stdout).toMatch(
115-
/error.*The parent tag of tag 'IMG-I-AMPHTML-INTRINSIC-SIZER' is 'div', but it can only be 'i-amphtml-sizer-intrinsic'/
116-
)
117-
expect(stdout).toMatch(
118-
/error.*The parent tag of tag 'IMG-I-AMPHTML-INTRINSIC-SIZER' is 'div', but it can only be 'i-amphtml-sizer-intrinsic'/
112+
/error.*The parent tag of tag 'img' is 'div', but it can only be 'i-amphtml-sizer-intrinsic'\./
119113
)
120114
await expect(access(join(outDir, 'dog-cat.html'))).resolves.toBe(
121115
undefined

0 commit comments

Comments
 (0)