Skip to content

Commit

Permalink
ignore loading pageMap for dynamic locale /[locale] (#3061)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina authored Jul 26, 2024
1 parent 04654df commit fef635e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-avocados-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nextra': patch
---

ignore loading pageMap for dynamic locale `/[locale]`
14 changes: 10 additions & 4 deletions packages/nextra/src/client/setup-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ function NextraLayout({

let { pageOpts, useTOC, Content } = pageContext

for (const { route, children } of __nextra_pageMap) {
const paths = route.split('/').slice(locale ? 2 : 1)
const folder = findFolder(pageOpts.pageMap, paths)
folder.children = children
const isDynamicLocale = route.startsWith('/[')

if (isDynamicLocale) {
pageOpts.pageMap = __nextra_pageMap
} else {
for (const { route, children } of __nextra_pageMap) {
const paths = route.split('/').slice(locale ? 2 : 1)
const folder = findFolder(pageOpts.pageMap, paths)
folder.children = children
}
}

if (__nextra_dynamic_opts) {
Expand Down
6 changes: 5 additions & 1 deletion packages/nextra/src/server/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,12 @@ export default MDXLayout`
const stringifiedPageOpts = JSON.stringify(pageOpts).slice(0, -1)
const pageMapPath = path.join(CHUNKS_DIR, `nextra-page-map-${locale}.mjs`)

const pageMap = locale.startsWith('[')
? 'const pageMap = []'
: `import { pageMap } from '${slash(pageMapPath)}'`

const rawJs = `import { HOC_MDXWrapper } from 'nextra/setup-page'
import { pageMap } from '${slash(pageMapPath)}'
${pageMap}
${isAppFileFromNodeModules ? cssImports : ''}
${finalResult}
Expand Down
47 changes: 0 additions & 47 deletions packages/nextra/src/server/webpack-plugins/nextra.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import path from 'node:path'
import gracefulFs from 'graceful-fs'
// import pkg from 'next/dist/compiled/webpack/webpack.js'
import type { Compiler } from 'webpack'
import type { NextraConfig } from '../../types'
import { CHUNKS_DIR, IS_PRODUCTION } from '../constants.js'
import { PAGES_DIR } from '../file-system.js'
import { collectPageMap } from '../page-map.js'

// import { logger } from '../utils'

// const { webpack, sources } = pkg
const fs = gracefulFs.promises

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

compiler.hooks.beforeCompile.tapAsync(pluginName, async (_, callback) => {
// if (isSaved || !IS_PRODUCTION) {
// // Never call hook 2 times
// // Also on `production` environment we get error:
// // Module not found: Can't resolve '.../.next/static/chunks/nextra-page-map-en.mjs'
// // while using only `processAssets` hook, but without `beforeCompile`
// callback()
// return
// }
if (IS_PRODUCTION && isSaved) {
callback()
return
Expand All @@ -61,46 +49,11 @@ export class NextraPlugin {
rawJs
)
}
// logger.info('`beforeCompile`')
isSaved = true
callback()
} catch (error) {
callback(error as Error)
}
})

// if (IS_PRODUCTION) {
// // Do not fire `processAssets` on production
// return
// }

// compiler.hooks.compilation.tap(pluginName, compilation => {
// compilation.hooks.processAssets.tapAsync(
// {
// name: pluginName,
// stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
// },
// async (assets, callback) => {
// try {
// // TODO: Find a way to get filename only for current asset? and get PageMap only for it
// for (const locale of locales) {
// const route = `/${locale}`
// const dir = PAGES_DIR + route
// const rawJs = await collectPageMap({ dir, route })
//
// const assetPath =
// (IS_PRODUCTION ? '../' : '') +
// `../static/chunks/nextra-page-map-${locale}.mjs`
//
// assets[assetPath] = new sources.RawSource(rawJs)
// }
// logger.info('`processAssets`')
// callback()
// } catch (error) {
// callback(error as Error)
// }
// }
// )
// })
}
}

0 comments on commit fef635e

Please sign in to comment.