Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sitemap generation if next/headers is used #759

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next-sitemap/src/builders/url-set-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class UrlSetBuilder {
? Object.keys(this.manifest?.preRender?.routes ?? {})
: []),
...(this.manifest?.staticExportPages ?? []),
...(this.manifest?.trace?.filter(e => !!e.tags.path).map(e => e.tags.path!) ?? []),
]

// Filter out next.js internal urls and generate urls based on sitemap
Expand Down
6 changes: 6 additions & 0 deletions packages/next-sitemap/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ export interface INextManifest {
preRender?: IPreRenderManifest
routes?: IRoutesManifest
staticExportPages?: string[]
trace?: INextTrace[]
}

export interface INextTrace {
tags: { path?: string }
}

/**
Expand Down Expand Up @@ -236,6 +241,7 @@ export interface IRuntimePaths {
SITEMAP_INDEX_FILE?: string
SITEMAP_INDEX_URL?: string
STATIC_EXPORT_ROOT: string
TRACE: string
}

export type IAlternateRef = {
Expand Down
5 changes: 5 additions & 0 deletions packages/next-sitemap/src/parsers/manifest-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
IRuntimePaths,
IRoutesManifest,
IConfig,
INextTrace,
} from '../interface.js'
import { Logger } from '../logger.js'
import { loadJSON } from '../utils/file.js'
Expand Down Expand Up @@ -70,11 +71,15 @@ export class ManifestParser {
this.runtimePaths.STATIC_EXPORT_ROOT,
)

// Load trace
const trace = await loadJSON<INextTrace[]>(this.runtimePaths.TRACE, true)

return {
build: buildManifest ?? ({} as any),
preRender: preRenderManifest,
routes: routesManifest,
staticExportPages,
trace,
}
}
}
17 changes: 16 additions & 1 deletion packages/next-sitemap/src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import path from 'node:path'
* @param throwError
* @returns
*/
export const loadJSON = async <T>(path: string): Promise<T | undefined> => {
export const loadJSON = async <T>(
path: string,
lineDelimited: boolean = false,
): Promise<T | undefined> => {
// Get path stat
const stat = await fs.stat(path).catch(() => {
return {
Expand All @@ -22,6 +25,18 @@ export const loadJSON = async <T>(path: string): Promise<T | undefined> => {

const jsonString = await fs.readFile(path, { encoding: 'utf-8' })

if (lineDelimited) {
const jsonLines = jsonString.split('\n')
return jsonLines
.map((line) => {
if (line.trim().length === 0) {
return []
}
return JSON.parse(line)
})
.reduce((accumulator, value) => accumulator.concat(value), [])
}

return JSON.parse(jsonString)
}

Expand Down
1 change: 1 addition & 0 deletions packages/next-sitemap/src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const getRuntimePaths = (config: IConfig): IRuntimePaths => {
STATIC_EXPORT_ROOT: getPath(config.outDir!),
SITEMAP_INDEX_URL,
SITEMAP_INDEX_FILE,
TRACE: getPath(config.sourceDir!, 'trace'),
}
}

Expand Down