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

Fix missing content-length header when fetching assets #961

Closed
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: 2 additions & 0 deletions packages/metro/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ class Server {
return data.slice(dataStart, dataEnd + 1);
}

res.setHeader('Content-Length', String(Buffer.byteLength(data)));

return data;
}

Expand Down
10 changes: 8 additions & 2 deletions packages/metro/src/Server/__tests__/Server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,22 +782,28 @@ describe('processRequest', () => {
expect(response.getHeader('content-range')).toBe('bytes 0-3/10');
});

it('should return content-type header for a png asset', async () => {
it('should return content-type and content-length header for a png asset', async () => {
const mockData = 'i am image';
getAsset.mockResolvedValue(mockData);

const response = await makeRequest('/assets/imgs/a.png?platform=ios');

expect(response.getHeader('content-type')).toBe('image/png');
expect(response.getHeader('content-length')).toBe(
String(Buffer.byteLength(mockData)),
);
});

it('should return content-type header for an svg asset', async () => {
it('should return content-type and content-length header for an svg asset', async () => {
const mockData = 'i am image';
getAsset.mockResolvedValue(mockData);

const response = await makeRequest('/assets/imgs/a.svg?platform=ios');

expect(response.getHeader('content-type')).toBe('image/svg+xml');
expect(response.getHeader('content-length')).toBe(
String(Buffer.byteLength(mockData)),
);
});

it("should serve assets files's name contain non-latin letter", async () => {
Expand Down