Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 62c0fe2

Browse files
authored
fix(core, lambda-at-edge): do not replace .html for public files in r… (#1473)
1 parent 14b828e commit 62c0fe2

File tree

7 files changed

+32
-7
lines changed

7 files changed

+32
-7
lines changed

packages/e2e-tests/next-app-with-base-path/cypress/integration/static-files.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ describe("Static Files Tests", () => {
1818
});
1919

2020
describe("public files", () => {
21-
[{ path: "/basepath/app-store-badge.png" }].forEach(({ path }) => {
21+
[
22+
{ path: "/basepath/app-store-badge.png", contentType: "image/png" },
23+
{ path: "/basepath/example.html", contentType: "text/html" }
24+
].forEach(({ path, contentType }) => {
2225
it(`serves and caches file ${path}`, () => {
2326
// Request once to ensure cached
2427
cy.request(path);
2528
cy.request(path).then((response) => {
26-
expect(response.headers["content-type"]).to.equal("image/png");
29+
expect(response.headers["content-type"]).to.equal(contentType);
2730
expect(response.status).to.equal(200);
2831
cy.verifyResponseCacheStatus(response, true);
2932
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<body>
4+
<h1>Serverless-next.js</h1>
5+
</body>
6+
</html>

packages/e2e-tests/next-app/cypress/integration/static-files.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ describe("Static Files Tests", () => {
1818
});
1919

2020
describe("public files", () => {
21-
[{ path: "/app-store-badge.png" }].forEach(({ path }) => {
21+
[
22+
{ path: "/app-store-badge.png", contentType: "image/png" },
23+
{ path: "/example.html", contentType: "text/html" }
24+
].forEach(({ path, contentType }) => {
2225
it(`serves and caches file ${path}`, () => {
2326
// Request once to ensure cached
2427
cy.request(path);
2528
cy.request(path).then((response) => {
26-
expect(response.headers["content-type"]).to.equal("image/png");
29+
expect(response.headers["content-type"]).to.equal(contentType);
2730
expect(response.status).to.equal(200);
2831
cy.verifyResponseCacheStatus(response, true);
2932
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<body>
4+
<h1>Serverless-next.js</h1>
5+
</body>
6+
</html>

packages/libs/core/src/route/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const handleLanguageRedirect = async (
7373
}
7474
};
7575

76-
const handlePublicFiles = (
76+
export const handlePublicFiles = (
7777
uri: string,
7878
manifest: Manifest
7979
): Route | undefined => {

packages/libs/lambda-at-edge/src/default-handler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
getCustomHeaders,
1515
StaticRoute,
1616
getStaticRegenerationResponse,
17-
getThrottledStaticRegenerationCachePolicy
17+
getThrottledStaticRegenerationCachePolicy,
18+
handlePublicFiles
1819
} from "@sls-next/core";
1920

2021
import {
@@ -138,6 +139,12 @@ const reconstructOriginalRequestUri = (
138139
s3Uri: string,
139140
manifest: OriginRequestDefaultHandlerManifest
140141
) => {
142+
// For public files we do not replace .html as it can cause public HTML files to be classified with wrong status code
143+
const publicFile = handlePublicFiles(s3Uri, manifest);
144+
if (publicFile) {
145+
return `${basePath}${s3Uri}`;
146+
}
147+
141148
let originalUri = `${basePath}${s3Uri.replace(
142149
/(\.html)?$/,
143150
manifest.trailingSlash ? "/" : ""

packages/libs/s3-static-assets/src/lib/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export const SERVER_NO_CACHE_CACHE_CONTROL_HEADER =
1111
"public, max-age=0, s-maxage=0, must-revalidate";
1212

1313
export const DEFAULT_PUBLIC_DIR_CACHE_REGEX =
14-
/\.(gif|jpe?g|jp2|tiff|png|webp|bmp|svg|ico)$/i;
14+
/\.(gif|jpe?g|jp2|tiff|png|webp|bmp|svg|ico|html)$/i;

0 commit comments

Comments
 (0)