#Sitemap Build Error

76 messages · Page 1 of 1 (latest)

mellow anchor
#

When viewing the sitemap built by my site, it generates the sitemap incorrectly. The following code is used to generate the sitemaps.

import { db } from '@/server/db';
import { MetadataRoute } from 'next';

export async function generateSitemaps() {
  return [{id: 0}, {id: 1}, {id: 2}];
}

export default async function sitemap({
  id: sitemapIndex
}: {
  id: number;
}): Promise<MetadataRoute.Sitemap> {
  // Google's limit is 50,000 URLs per sitemap
  const start = sitemapIndex * 50000;
  const end = start + 50000;
  const addressSlugs = await db.property.findMany({
    where: { addressSlug: { not: null } },
    skip: start, // Skip to the starting index
    take: end - start, // Take the number of properties from start to end
    orderBy: {
      id: 'asc'
    },
    select: { addressSlug: true, updatedAt: true }
  });

  // The addressSlugs field is verified to have all addressSlug fields a string
  return addressSlugs.map(({ addressSlug, updatedAt }) => ({
    url: `https://www.mfvalue.com/estimator/${addressSlug}`,
    lastModified: updatedAt || new Date()
  }));
}

The error can be found here: https://www.mfvalue.com/estimator/sitemap/0.xml

gilded impBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

stiff nest
mellow anchor
gilded impBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer

[Click here](#1226737436818935879 message)

mellow anchor
#

In their docs it seems like you cannot generate them dynamically

#

Is there a proper way to do it?

stiff nest
#

if you have big site, it's better to generate a sitemap index and contain all the link of other sitemap in the index

#

you could also use route handler with dynamic route to make the sitemap but return text/xml instead

mellow anchor
#

Is there an option to make it specify sitemap links in xml

stiff nest
#
mellow anchor
#

Can it generate the index sitemap?

mellow anchor
#
:sparkles: [next-sitemap] Loading next-sitemap config: file:///vercel/path0/frontend/next-sitemap.config.js
 :x: [next-sitemap] Error: Cannot find module '@/env'
Require stack:
- /vercel/path0/frontend/next-sitemap.config.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1134:15)
    at Module._load (node:internal/modules/cjs/loader:975:27)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (/vercel/path0/frontend/next-sitemap.config.js:1:17)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:202:29) {
stiff nest
mellow anchor
mellow anchor
#

yes

stiff nest
#

const { db } = require('./src/server/db')

mellow anchor
#
 :x: [next-sitemap] /vercel/path0/frontend/next-sitemap.config.js:1
import { db } from '@/server/db';
^^^^^^
SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1274:20)
    at Module._compile (node:internal/modules/cjs/loader:1320:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:202:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:336:24)
    at async ConfigParser.loadBaseConfig (file:///vercel/path0/frontend/node_modules/next-sitemap/dist/esm/parsers/config-parser.js:52:28)
stiff nest
#

you can't use import there

mellow anchor
#
✨ [next-sitemap] Loading next-sitemap config: file:///vercel/path0/frontend/next-sitemap.config.js
 ❌ [next-sitemap] Error: Cannot find module './src/server/db'
Require stack:
- /vercel/path0/frontend/next-sitemap.config.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1134:15)
    at Module._load (node:internal/modules/cjs/loader:975:27)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (/vercel/path0/frontend/next-sitemap.config.js:1:16)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:202:29) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/vercel/path0/frontend/next-sitemap.config.js' ]
}
#

atleast it tries to import

#

The path is valid, why wouldn't it import?

mellow anchor
#

Unless the file is run elsewhere?

stiff nest
mellow anchor
#

to generate sitemaps

#

isnt that what you were referring me to use for creating the sitemaps instead?

stiff nest
mellow anchor
#

That uses an api route though.

#

Every time the sitemap is fetched, I need to do a DB call which is super expensive

#

as in I need to fetch 2 million records

mellow anchor
stiff nest
#

it is a route handler

mellow anchor
#

But doesnt the route need to generate the index sitemap every single time it is called?

stiff nest
#

no

mellow anchor
#

Normal route handler data cannot be statically generated at build time

stiff nest
#

no

mellow anchor
#

so its not an api route?

stiff nest
#

GET handler is static by default

#

well you can always try it

#

just try it with 10 record

mellow anchor
#

but how long will it cache for?

#

How does it know when the data from database changes to revalidate

#

I just want it to only run once at build time, refreshing each buildtime

stiff nest
#

until you rebuild

#

or use revalidatePath

mellow anchor
#

Is the behavior you describe applicable to all route handlers?

stiff nest
#

you can do this in a route.ts file if you like

export const dynamic = 'force-static'
#

and you should see that route is static in the build report after the build process

mellow anchor
#

very interesting

#

what if the route handler uses the arguments given? Does Nextjs have a way to handle various inputs too without returning the same data?

stiff nest
mellow anchor
#

How does it know?

#

is it through this:
export const dynamic = 'force-static'

stiff nest
#

yes

#

read the doc how to opt-out

mellow anchor
#

I do notice that querying the /server-sitemap-index.xml page doesnt show any logs in the vercel logs

#

How can that be?

stiff nest
#

what log are you expecting?

mellow anchor
mellow anchor
stiff nest
#

I don't think static route will show in here

mellow anchor
#

I didnt use export const dynamic = 'force-static'