Hivekind / hivekind.com

0 stars 0 forks source link

sitemap via app/sitemap.xml/route.ts and generate top level URL dynamically #80

Closed shuwnyuantee closed 3 months ago

shuwnyuantee commented 3 months ago

This ticket is created to solve the below issues:

Issue 1:

Initially we implemented sitemap generation via app.sitemap.ts, which is a Next.js convention in /app router:

https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap

During deployment (npm run build), sitemap.xml was generated correctly, and accessible at: https://hivekind-com.pages.dev/sitemap.xml

But in development, accessing it via localhost:3000/sitemap.xml failed, with below error:

For deployment build (npm run build), the sitemap is generated without any issue.
But on dev env (npm run dev), it always show this error:
⨯ Error: Page "/sitemap.xml/[[...__metadata_id__]]/route" is missing exported function "generateStaticParams()", which is required with "output: export" config.
    at DevServer.renderToResponseWithComponentsImpl (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/base-server.js:1082:27)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async DevServer.renderPageComponent (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/base-server.js:1931:24)
    at async DevServer.renderToResponseImpl (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/base-server.js:1969:32)
    at async DevServer.pipeImpl (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/base-server.js:920:25)
    at async NextNodeServer.handleCatchallRenderRequest (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/next-server.js:272:17)
    at async DevServer.handleRequestImpl (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/base-server.js:816:17)
    at async /Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/dev/next-dev-server.js:339:20
    at async Span.traceAsyncFn (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/trace/trace.js:154:20)
    at async DevServer.handleRequest (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/dev/next-dev-server.js:336:24)
    at async invokeRender (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/lib/router-server.js:174:21)
    at async handleRequest (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/lib/router-server.js:353:24)
    at async requestHandlerImpl (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/lib/router-server.js:377:13)
    at async Server.requestListener (/Users/shuwnyuan/hivekind.com/node_modules/next/dist/server/lib/start-server.js:141:13) {
  page: '/sitemap.xml'
}

Similar issue was discussed here, but no solution at the moment: https://github.com/vercel/next.js/issues/59136 https://github.com/diverta/front_next_media/pull/32

In order to get rid of the error in dev mode, we have go with this workaround as mentioned here:

https://github.com/vercel/next.js/issues/59136#issuecomment-1938195038

Here's the Next.js doc for this approach: https://nextjs.org/docs/app/building-your-application/routing/route-handlers#non-ui-responses

Issue 2:

Initially for top level static URLs (eg: /careers, /work, /blog), we created a hardcoded array for that. This is inconvenient, as whenever a new page is added, this array has to be updated manually.

Next.js doesn't provide a way to give a list of top level URLs (which correspond to sub dir name inside /app). So here we implemented our own way of parsing the /app structure, grab a list of dir name as the top level URLs.