Skip to content

Commit fef635e

Browse files
authoredJul 26, 2024··
ignore loading pageMap for dynamic locale /[locale] (#3061)
1 parent 04654df commit fef635e

File tree

4 files changed

+20
-52
lines changed

4 files changed

+20
-52
lines changed
 

‎.changeset/smart-avocados-raise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'nextra': patch
3+
---
4+
5+
ignore loading pageMap for dynamic locale `/[locale]`

‎packages/nextra/src/client/setup-page.tsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,16 @@ function NextraLayout({
6060

6161
let { pageOpts, useTOC, Content } = pageContext
6262

63-
for (const { route, children } of __nextra_pageMap) {
64-
const paths = route.split('/').slice(locale ? 2 : 1)
65-
const folder = findFolder(pageOpts.pageMap, paths)
66-
folder.children = children
63+
const isDynamicLocale = route.startsWith('/[')
64+
65+
if (isDynamicLocale) {
66+
pageOpts.pageMap = __nextra_pageMap
67+
} else {
68+
for (const { route, children } of __nextra_pageMap) {
69+
const paths = route.split('/').slice(locale ? 2 : 1)
70+
const folder = findFolder(pageOpts.pageMap, paths)
71+
folder.children = children
72+
}
6773
}
6874

6975
if (__nextra_dynamic_opts) {

‎packages/nextra/src/server/loader.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,12 @@ export default MDXLayout`
211211
const stringifiedPageOpts = JSON.stringify(pageOpts).slice(0, -1)
212212
const pageMapPath = path.join(CHUNKS_DIR, `nextra-page-map-${locale}.mjs`)
213213

214+
const pageMap = locale.startsWith('[')
215+
? 'const pageMap = []'
216+
: `import { pageMap } from '${slash(pageMapPath)}'`
217+
214218
const rawJs = `import { HOC_MDXWrapper } from 'nextra/setup-page'
215-
import { pageMap } from '${slash(pageMapPath)}'
219+
${pageMap}
216220
${isAppFileFromNodeModules ? cssImports : ''}
217221
${finalResult}
218222
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import path from 'node:path'
22
import gracefulFs from 'graceful-fs'
3-
// import pkg from 'next/dist/compiled/webpack/webpack.js'
43
import type { Compiler } from 'webpack'
54
import type { NextraConfig } from '../../types'
65
import { CHUNKS_DIR, IS_PRODUCTION } from '../constants.js'
76
import { PAGES_DIR } from '../file-system.js'
87
import { collectPageMap } from '../page-map.js'
98

10-
// import { logger } from '../utils'
11-
12-
// const { webpack, sources } = pkg
139
const fs = gracefulFs.promises
1410

1511
let isSaved = false
@@ -27,14 +23,6 @@ export class NextraPlugin {
2723
const { locales, transformPageMap } = this.config
2824

2925
compiler.hooks.beforeCompile.tapAsync(pluginName, async (_, callback) => {
30-
// if (isSaved || !IS_PRODUCTION) {
31-
// // Never call hook 2 times
32-
// // Also on `production` environment we get error:
33-
// // Module not found: Can't resolve '.../.next/static/chunks/nextra-page-map-en.mjs'
34-
// // while using only `processAssets` hook, but without `beforeCompile`
35-
// callback()
36-
// return
37-
// }
3826
if (IS_PRODUCTION && isSaved) {
3927
callback()
4028
return
@@ -61,46 +49,11 @@ export class NextraPlugin {
6149
rawJs
6250
)
6351
}
64-
// logger.info('`beforeCompile`')
6552
isSaved = true
6653
callback()
6754
} catch (error) {
6855
callback(error as Error)
6956
}
7057
})
71-
72-
// if (IS_PRODUCTION) {
73-
// // Do not fire `processAssets` on production
74-
// return
75-
// }
76-
77-
// compiler.hooks.compilation.tap(pluginName, compilation => {
78-
// compilation.hooks.processAssets.tapAsync(
79-
// {
80-
// name: pluginName,
81-
// stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
82-
// },
83-
// async (assets, callback) => {
84-
// try {
85-
// // TODO: Find a way to get filename only for current asset? and get PageMap only for it
86-
// for (const locale of locales) {
87-
// const route = `/${locale}`
88-
// const dir = PAGES_DIR + route
89-
// const rawJs = await collectPageMap({ dir, route })
90-
//
91-
// const assetPath =
92-
// (IS_PRODUCTION ? '../' : '') +
93-
// `../static/chunks/nextra-page-map-${locale}.mjs`
94-
//
95-
// assets[assetPath] = new sources.RawSource(rawJs)
96-
// }
97-
// logger.info('`processAssets`')
98-
// callback()
99-
// } catch (error) {
100-
// callback(error as Error)
101-
// }
102-
// }
103-
// )
104-
// })
10558
}
10659
}

0 commit comments

Comments
 (0)
Please sign in to comment.