vercel / next.js

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

Sitemap.xml not cached. #61616

Open dsosborn opened 10 months ago

dsosborn commented 10 months ago

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/vibrant-river-l88glf?workspaceId=9084caf4-7fa0-449a-ae87-c3bec4f52e15

To Reproduce

Add sitemap.ts file to app directory. Build and start server.

Current vs. Expected behavior

The current behavior is that the /sitemap.xml file is not cached and served with Cache-Control header as public, max-age=0, must-revalidate requiring the server to be hit with each request for /sitemap.xml.

I would expect to use the same cache functionality in the sitemap file as in page.tsx files.

I.e. if there is nothing dynamic it would be permanently cached, or cached according to a revalidate value exported from the file, e.g. export const revalidate = 3600;. Additionally, I would expect to be able to fetch data from outside sources and have those fetches cached like it is in page.tsx files with the page cache matching the shortest fetch time.

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:27:27 PDT 2023; root:xnu-10002.41.9~6/RELEASE_X86_64
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.1.1-canary.27 // Latest available version is detected (14.1.1-canary.27).
  eslint-config-next: 14.0.4
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: standalone

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

App Router

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

next build (local), next start (local)

Additional context

No response

tcxseo commented 4 months ago

It sounds like you're dealing with an issue where your sitemap.xml file is not being cached. There could be a few reasons for this, such as server settings, caching rules, or problems with the file itself. Here are a few steps you can take to troubleshoot and resolve this issue:

Check Server Caching Settings: Ensure that your server is configured to cache the sitemap.xml file. This typically involves setting appropriate cache headers.

Verify .htaccess or Nginx Configuration: If you are using Apache, check your .htaccess file for any rules that might be preventing caching. For Nginx, verify your configuration files.

Example for Apache:

<FilesMatch "sitemap.xml"> Header set Cache-Control "max-age=3600, public"

Example for Nginx:

location = /sitemap.xml { expires 1h; add_header Cache-Control "public"; }

Check for Caching Plugin or CDN Settings: If you are using a Content Delivery Network (CDN) or a caching plugin (for CMS platforms like WordPress), ensure that the sitemap.xml file is included in the caching rules.

Verify File Accessibility: Make sure the sitemap.xml file is accessible and not restricted by permissions. You can check this by visiting http://yourdomain.com/sitemap.xml in your browser.

Clear Cache: If changes have been made, clear any existing caches to ensure the new settings take effect. This includes server cache, browser cache, and CDN cache.

Test Caching: Use tools like curl or browser developer tools to check the response headers for your sitemap.xml file to confirm that caching is working.

Example with curl:

curl -I http://yourdomain.com/sitemap.xml

Look for headers like Cache-Control and Expires in the response.

By following these steps, you should be able to identify and fix the issue with your sitemap.xml file not being cached. If you need further assistance, please provide more details about your server environment and any caching mechanisms you are using.

(https://www.ravi-gupta.com/)

keifgwinn commented 3 weeks ago

I'm seeing a similiar thing, coming straight out of the next static server.

I'm currently using "next": "14.2.16", which uses https://github.com/pillarjs/send/releases/tag/0.19.0 underneath. running with DEBUG="send node app"

I max-age=0 on files out of the /.next/ path or this /public/wa path

app-1 | 16:22:44 0|WebApp | 2024-11-08T16:22:44.498Z send stat "/app/public/wa/LogoWhite.svg" app-1 | 16:22:44 0|WebApp | 2024-11-08T16:22:44.502Z send pipe "/app/public/wa/LogoWhite.svg" app-1 | 16:22:44 0|WebApp | 2024-11-08T16:22:44.502Z send accept ranges app-1 | 16:22:44 0|WebApp | 2024-11-08T16:22:44.502Z send cache-control public, max-age=0 app-1 | 16:22:44 0|WebApp | 2024-11-08T16:22:44.502Z send modified Mon, 02 Sep 2024 09:09:33 GMT app-1 | 16:22:44 0|WebApp | 2024-11-08T16:22:44.503Z send etag W/"19c0-191b1ff20c8" app-1 | 16:22:44 0|WebApp | 2024-11-08T16:22:44.503Z send content-type image/svg+xml

On my own sitemap.xml.ts route, I was setting the headers by hand

res.setHeader('Content-Type', 'application/xml'); res.setHeader( 'Cache-Control', 's-maxage=86400, public, max-age=86400, stale-while-revalidate' );

and that's working, I'm getting the right results