vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.86k stars 26.86k forks source link

Error Generating Sitemap with "output: export" config #59136

Open belei-bohdan opened 10 months ago

belei-bohdan commented 10 months ago

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/generate-sitemap-with-output-export-h5v4vg

To Reproduce

  1. Open the CodeSandbox sample
  2. Access url within the sandboxed environment by navigating to https://h5v4vg-3000.csb.app/sitemap.xml

Current vs. Expected behavior

Description:

Attempting to generate a sitemap using the provided example from the official Next.js documentation results in an error when the output is set to "export". The code functions correctly with output set to "standalone".

Code Used:

// app/sitemap.js
export default async function sitemap() {
    return [
        {
            url: 'localhost:3000/',
            lastModified: new Date(),
            changeFrequency: 'yearly',
            priority: 1,
        },
       // ...other URLs
    ]
}

Next.js Configuration:

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: "export",
};

module.exports = nextConfig;

Current Behavior:

Accessing the "/sitemap.xml" endpoint results in a Server Error:

Error: Page "/sitemap.xml/[[...__metadata_id__]]/route" is missing exported function "generateStaticParams()", which is required with "output: export" config.

Expected Behavior:

Accessing "/sitemap.xml" should display the generated sitemap page without errors.

Verify canary release

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
Binaries:
  Node: 20.6.1
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.0.4-canary.28
  eslint-config-next: 14.0.3
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 4.9.5
Next.js Config:
  output: export

Which area(s) are affected? (Select all that apply)

Metadata (metadata, generateMetadata, next/head), Static HTML Export (output: "export")

Additional context

No response

JerryWu1234 commented 10 months ago

May I try it ?

pyroblue commented 10 months ago

I am also experiencing this as well. I've tried several combinations of generateStaticParams() to no avail.

JerryWu1234 commented 10 months ago

image because of here. I have to deep into this more, and get how to fix it. I'm a new, so I have more time

pyroblue commented 10 months ago

Curious if you were able to dig in any at all @JerryWu1234 - I haven't had the time, but this is pretty darn frustrating to say the least 😄

JerryWu1234 commented 10 months ago

Curious if you were able to dig in any at all @JerryWu1234 - I haven't had the time, but this is pretty darn frustrating to say the least 😄

I'm digging in right now. because nextjs was big project. of course if you have any tips, it be well.

sushantdhiman commented 8 months ago

For now you can use app route for this

/** app/sitemap.xml/route.ts **/

// dynamically generate sitemap xml data
function getSitemap() {
  const map = [
    {
      url: 'https://my.app.url',
      lastModified: new Date(),
      changeFrequency: 'daily',
      priority: 1,
    }
  ];

  return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    ${map
      .map(
        (item) => `
            <url>
              <loc>${item.url}</loc>
              <lastmod>${item.lastModified.toISOString()}</lastmod>
              <changefreq>${item.changeFrequency}</changefreq>
              <priority>${item.priority}</priority>
            </url>
          `,
      )
      .join('')}
    </urlset>
  `;
}

export async function GET() {
  return new Response(getSitemap(), {
    headers: {
      'Content-Type': 'text/xml',
    },
  });
}
modelga commented 8 months ago

@sushantdhiman looks like workaround but seems to be working one ;)

But another question. Does it work in export mode ?

sushantdhiman commented 8 months ago

@modelga Yes, it will generate sitemap.xml file in the out folder

lhfmartin commented 7 months ago

Looks like the issue affects dev server only. If a build is triggered it is still able to generate the sitemap.xml.

fosple commented 2 weeks ago

Same issue here, only affects the dev server (npm run dev). Is there a plan to fix it?