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: bandwidth issues for 404 resources #6302 #6341

Merged
merged 9 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion packages/qwik-city/middleware/azure-swa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import { getNotFound } from '@qwik-city-not-found-paths';
import { _deserializeData, _serializeData, _verifySerializable } from '@builder.io/qwik';
import { parseString } from 'set-cookie-parser';
import { isStaticPath } from '@qwik-city-static-paths';

// @builder.io/qwik-city/middleware/azure-swa

Expand Down Expand Up @@ -123,7 +124,13 @@ export function createQwikCity(opts: QwikCityAzureOptions): AzureFunction {

// qwik city did not have a route for this request
// response with 404 for this pathname
const notFoundHtml = getNotFound(url.pathname);

// In the development server, we replace the getNotFound function
// For static paths, we assign a static "Not Found" message.
// This ensures consistency between development and production environments for specific URLs.
const notFoundHtml = isStaticPath(req.method || 'GET', url)
? 'Not Found'
: getNotFound(url.pathname);
return {
status: 404,
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },
Expand Down
8 changes: 7 additions & 1 deletion packages/qwik-city/middleware/bun/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ export function createQwikCity(opts: QwikCityBunOptions) {
const notFound = async (request: Request) => {
try {
const url = new URL(request.url);
const notFoundHtml = getNotFound(url.pathname);

// In the development server, we replace the getNotFound function
// For static paths, we assign a static "Not Found" message.
// This ensures consistency between development and production environments for specific URLs.
const notFoundHtml = isStaticPath(request.method || 'GET', url)
? 'Not Found'
: getNotFound(url.pathname);
return new Response(notFoundHtml, {
status: 404,
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },
Expand Down
8 changes: 7 additions & 1 deletion packages/qwik-city/middleware/cloudflare-pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ export function createQwikCity(opts: QwikCityCloudflarePagesOptions) {

// qwik city did not have a route for this request
// response with 404 for this pathname
const notFoundHtml = getNotFound(url.pathname);

// In the development server, we replace the getNotFound function
// For static paths, we assign a static "Not Found" message.
// This ensures consistency between development and production environments for specific URLs.
const notFoundHtml = isStaticPath(request.method || 'GET', url)
? 'Not Found'
: getNotFound(url.pathname);
return new Response(notFoundHtml, {
status: 404,
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },
Expand Down
8 changes: 7 additions & 1 deletion packages/qwik-city/middleware/deno/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ export function createQwikCity(opts: QwikCityDenoOptions) {
const notFound = async (request: Request) => {
try {
const url = new URL(request.url);
const notFoundHtml = getNotFound(url.pathname);

// In the development server, we replace the getNotFound function
// For static paths, we assign a static "Not Found" message.
// This ensures consistency between development and production environments for specific URLs.
const notFoundHtml = isStaticPath(request.method || 'GET', url)
? 'Not Found'
: getNotFound(url.pathname);
return new Response(notFoundHtml, {
status: 404,
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },
Expand Down
8 changes: 7 additions & 1 deletion packages/qwik-city/middleware/netlify-edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ export function createQwikCity(opts: QwikCityNetlifyOptions) {

// qwik city did not have a route for this request
// response with 404 for this pathname
const notFoundHtml = getNotFound(url.pathname);

// In the development server, we replace the getNotFound function
// For static paths, we assign a static "Not Found" message.
// This ensures consistency between development and production environments for specific URLs.
const notFoundHtml = isStaticPath(request.method || 'GET', url)
? 'Not Found'
: getNotFound(url.pathname);
return new Response(notFoundHtml, {
status: 404,
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },
Expand Down
12 changes: 9 additions & 3 deletions packages/qwik-city/middleware/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ export function createQwikCity(opts: QwikCityNodeRequestOptions) {
if (!res.headersSent) {
const origin = computeOrigin(req, opts);
const url = getUrl(req, origin);
const notFoundHtml = getNotFound(url.pathname);

// In the development server, we replace the getNotFound function
// For static paths, we assign a static "Not Found" message.
// This ensures consistency between development and production environments for specific URLs.
const notFoundHtml = isStaticPath(req.method || 'GET', url)
? 'Not Found'
: getNotFound(url.pathname);
res.writeHead(404, {
'Content-Type': 'text/html; charset=utf-8',
'X-Not-Found': url.pathname,
Expand Down Expand Up @@ -105,8 +111,9 @@ export function createQwikCity(opts: QwikCityNodeRequestOptions) {
} else {
filePath = join(staticFolder, pathname, 'index.html');
}
const stream = createReadStream(filePath);
const ext = extname(filePath).replace(/^\./, '');
const stream = createReadStream(filePath);
stream.on('error', next);

const contentType = MIME_TYPES[ext];

Expand All @@ -118,7 +125,6 @@ export function createQwikCity(opts: QwikCityNodeRequestOptions) {
res.setHeader('Cache-Control', opts.static.cacheControl);
}

stream.on('error', next);
stream.pipe(res);

return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* DO NOT CHANGE THE EXACT VALUE OF `Resource Not Found`, THIS WILL BE REPLACED IN THE CODE LATER,
* DEPENDING ON RUNTIME (e.g. DEV or PRODUCTION)
*/
export function getNotFound(_pathname: string) {
return 'Resource Not Found';
}
8 changes: 7 additions & 1 deletion packages/qwik-city/middleware/vercel-edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ export function createQwikCity(opts: QwikCityVercelEdgeOptions) {

// qwik city did not have a route for this request
// response with 404 for this pathname
const notFoundHtml = getNotFound(url.pathname);

// In the development server, we replace the getNotFound function
// For static paths, we assign a static "Not Found" message.
// This ensures consistency between development and production environments for specific URLs.
const notFoundHtml = isStaticPath(request.method || 'GET', url)
? 'Not Found'
: getNotFound(url.pathname);
return new Response(notFoundHtml, {
status: 404,
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },
Expand Down
14 changes: 14 additions & 0 deletions starters/e2e/qwikcity/adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from "@playwright/test";

test.describe("Qwik City Adapter", () => {
test("Qwik City Adapter", async ({ page: api }) => {
const rsp = (await api.goto("/qwikcity-test/build/a-random-file-after-that.js"))!;
expect(rsp.status()).toBe(200);
expect(rsp.headers()["content-type"]).toBe(
"text/html; charset=utf-8",
);

const data = await rsp.text();
expect(data).toBe("Not Found");
});
});
Loading