Skip to content

Commit 19073cd

Browse files
committed
chore: update proxy example
1 parent 2f32abd commit 19073cd

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed

docs/explanation/sitemaps.md

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,69 @@ To address those, there are some proposed solutions for headless sitemap impleme
2626

2727
1. **Proxying Sitemaps from Backend to Frontend**
2828

29-
Approach: This approach maintains WordPress's native sitemap generation capabilities while ensuring proper frontend URLs are used. It involves creating a route in your frontend application that proxies requests to the WordPress backend sitemap.
29+
Approach: This approach maintains WordPress's native sitemap generation capabilities while ensuring proper frontend URLs are used. It involves creating API routes in your Next.js application that proxy requests to the WordPress backend sitemap.
3030

3131
Example Code:
3232

3333
```javascript
34-
// pages/[...sitemap].js
35-
export async function GET(request, { params }) {
36-
const { sitemap } = params;
37-
const sitemapFile = Array.isArray(sitemap) ? sitemap.join('/') : sitemap;
38-
39-
const wpSitemapUrl = `${process.env.WORDPRESS_URL}/wp-sitemap${sitemapFile ? `-${sitemapFile}` : ''}.xml`;
40-
34+
// /pages/api/proxy-sitemap/[...slug].js
35+
const wordpressUrl = (
36+
process.env.NEXT_PUBLIC_WORDPRESS_URL || "http://localhost:8888"
37+
).trim();
38+
39+
export default async function handler(req, res) {
40+
const slug = req.query.slug || [];
41+
42+
// Reconstruct the original WordPress sitemap path
43+
let wpUrl;
44+
if (slug.length === 0 || slug[0] === "sitemap.xml") {
45+
wpUrl = `${wordpressUrl}/sitemap.xml`;
46+
} else {
47+
const wpPath = slug.join("/");
48+
wpUrl = `${wordpressUrl}/${wpPath}.xml`;
49+
}
50+
51+
console.debug("Fetching sitemap", wpUrl);
4152
try {
42-
const response = await fetch(wpSitemapUrl);
43-
const sitemapContent = await response.text();
44-
45-
const transformedContent = sitemapContent.replace(
46-
new RegExp(process.env.WORDPRESS_URL, 'g'),
47-
process.env.FRONTEND_URL
48-
);
49-
50-
return new Response(transformedContent, {
51-
headers: {
52-
'Content-Type': 'application/xml',
53-
},
54-
});
55-
} catch (error) {
56-
console.error('Error fetching sitemap:', error);
57-
return new Response('Sitemap not found', { status: 404 });
53+
const wpRes = await fetch(wpUrl);
54+
console.debug("Fetching sitemap", wpRes);
55+
if (!wpRes.ok) {
56+
return res.status(wpRes.status).send("Error fetching original sitemap");
57+
}
58+
59+
const contentType = wpRes.headers.get("content-type") || "application/xml";
60+
let body = await wpRes.text();
61+
// Remove XML stylesheets if present
62+
body = body.replace(/<\?xml-stylesheet[^>]*\?>\s*/g, "");
63+
64+
res.setHeader("Content-Type", contentType);
65+
res.status(200).send(body);
66+
} catch (err) {
67+
res.status(500).send("Internal Proxy Error");
5868
}
5969
}
6070
```
6171

72+
Then add the necessary rewrites in your `next.config.js`:
73+
```javascript
74+
module.exports = {
75+
async rewrites() {
76+
return [
77+
{
78+
source: "/:path(wp-sitemap-.*).xml",
79+
destination: "/api/proxy-sitemap/:path",
80+
},
81+
{
82+
source: "/sitemap.xml",
83+
destination: "/api/proxy-sitemap/sitemap.xml",
84+
},
85+
];
86+
},
87+
// other Next.js configuration
88+
};
89+
```
90+
This implementation ensures that when visitors access `/sitemap.xml` on your headless frontend, they'll see the WordPress sitemap content.
91+
6292
This route will serve the WordPress `sitemap.xml` in your Next.js application dynamically.
6393

6494
- **Pros**
@@ -71,7 +101,7 @@ This route will serve the WordPress `sitemap.xml` in your Next.js application dy
71101
* Limited flexibility for custom frontend routes not defined in WordPress
72102
* Requires proper URL transformation to replace backend URLs with frontend URLs
73103
* May require additional handling for caching and performance
74-
* May propagate any errors experienced in WordPress when proxying the sitemap.xml
104+
* May propagate any errors experienced in WordPress when proxying the `sitemap.xml`
75105

76106
2. **Generating a Sitemap from GraphQL Content**
77107

0 commit comments

Comments
 (0)