vercel / next.js

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

Sitemap.xml not cached. #61616

Open dsosborn opened 7 months ago

dsosborn commented 7 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 2 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/)