Skip to content

Commit

Permalink
[Next.js] Simplify sitemap.xml handling
Browse files Browse the repository at this point in the history
  • Loading branch information
illiakovalenko committed Jan 28, 2025
1 parent 9b0b19a commit 50d1dba
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,9 @@ 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();
res.send(xmlResponse.data);
} catch (error) {
return res.redirect('/404');
}
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

0 comments on commit 50d1dba

Please sign in to comment.