Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Next.js] sitemap.xml endpoint returns 404 #2023

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Our versioning strategy is as follows:
### 🐛 Bug Fixes

* `[sitecore-jss-nextjs]` Fix Chromes editing mode when rendering host URL is internally redirected in XMCloud ([#2019](https://github.com/Sitecore/jss/pull/2019))
* `[sitecore-jss]` NativeDataFetcher doesn't return stream response ([#2020](https://github.com/Sitecore/jss/pull/2020))
* `[templates/next.js]` sitemap.xml endpoint returns 404 ([#2023](https://github.com/Sitecore/jss/pull/2023))

## 22.4.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,12 @@ const sitemapApi = async (

try {
const fetcher = new NativeDataFetcher();
const response = await fetcher.fetch<ReadableStream<Uint8Array>>(sitemapUrl);
const xmlResponse = await fetcher.fetch<string>(sitemapUrl);

const reader = response.data.getReader();
if (reader) {
while (true) {
const { done, value } = await reader.read();

if (done) break;
if (value) res.write(value);
}
}

res.end();
return res.send(xmlResponse.data);
} catch (error) {
return res.redirect('/404');
}

return;
}

// index /sitemap.xml that includes links to all sitemaps
Expand Down
24 changes: 0 additions & 24 deletions packages/sitecore-jss/src/native-fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,30 +136,6 @@ describe('NativeDataFetcher', () => {
expect(fetchInit?.body).to.be.undefined;
});

it('should execute request with stream response type', async () => {
const fetcher = new NativeDataFetcher();

const fakeRes = { body: new ReadableStream() };

spy.on(
global,
'fetch',
mockFetch(200, fakeRes, {
customHeaders: {
'Content-Type': 'text/xml',
},
})
);

const response = await fetcher.fetch('http://test.com/api');

expect(response.status).to.equal(200);
expect(response.data instanceof ReadableStream).to.be.true;
expect(fetchInput).to.equal('http://test.com/api');
expect(fetchInit?.method).to.equal('GET');
expect(fetchInit?.body).to.be.undefined;
});

it('should execute POST request with data', async () => {
const fetcher = new NativeDataFetcher();
const postData = { x: 'val1', y: 'val2' };
Expand Down
5 changes: 1 addition & 4 deletions packages/sitecore-jss/src/native-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,12 @@ export class NativeDataFetcher {
debug: (message: string, ...optionalParams: any[]) => void
): Promise<unknown> {
const contentType = response.headers.get('Content-Type') || '';

try {
if (contentType.includes('application/json')) {
return await response.json();
}

if (response.body instanceof ReadableStream) {
return response.body;
}

return await response.text();
} catch (error) {
debug('Response parsing error: %o', error);
Expand Down