Skip to content
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: 2 additions & 0 deletions packages/website-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"main": "index.ts",
"scripts": {
"dev": "wrangler dev",
"test": "node --import @oxc-node/core/register --test \"src/**/*.test.ts\"",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"toucan-js": "3.4.0"
},
"devDependencies": {
"@cloudflare/workers-types": "4.20250321.0",
"@oxc-node/core": "^0.0.35",
"@types/node": "20.17.12",
"typescript": "5.7.3",
"wrangler": "3.78.7"
Expand Down
2 changes: 2 additions & 0 deletions packages/website-router/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type RewriteRecord = {
rewrite: string;
preserveSearch?: boolean;
crisp?: { segments: string[] };
sitemap?: boolean;
banner?: string;
Expand Down Expand Up @@ -102,6 +103,7 @@ export const jsonConfig = {
},
'/graphql/hive-testing': {
rewrite: 'hive-platform-docs.theguild.workers.dev',
preserveSearch: true,
crisp: { segments: ['hive-website'] },
sitemap: false,
},
Expand Down
37 changes: 37 additions & 0 deletions packages/website-router/src/routing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference types="node" />

import { equal } from 'node:assert/strict';
import { test } from 'node:test';
import { buildUpstreamUrl } from './routing';

test('preserveSearch keeps the original query string', () => {
const upstreamUrl = buildUpstreamUrl({
request: new Request(
'https://the-guild.dev/graphql/hive-testing/_serverFn/test?payload=%7B%22foo%22%3A1%7D',
),
record: {
preserveSearch: true,
rewrite: 'hive-platform-docs.theguild.workers.dev',
},
upstreamPath: '/_serverFn/test',
});

equal(
upstreamUrl.toString(),
'https://hive-platform-docs.theguild.workers.dev/_serverFn/test?payload=%7B%22foo%22%3A1%7D',
);
});

test('default rewrite behavior still drops search params', () => {
const upstreamUrl = buildUpstreamUrl({
request: new Request(
'https://the-guild.dev/graphql/hive-testing/_serverFn/test?payload=%7B%22foo%22%3A1%7D',
),
record: {
rewrite: 'hive-platform-docs.theguild.workers.dev',
},
upstreamPath: '/_serverFn/test',
});

equal(upstreamUrl.toString(), 'https://hive-platform-docs.theguild.workers.dev/_serverFn/test');
});
16 changes: 15 additions & 1 deletion packages/website-router/src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ export function redirect(sentry: Toucan, from: string, url: string, code = 301)

export type ManipulateResponseFn = (record: RewriteRecord, response: Response) => Promise<Response>;

export function buildUpstreamUrl(options: {
request: Request;
record: RewriteRecord;
upstreamPath: string;
}) {
const upstreamUrl = new URL(`https://${options.record.rewrite}${options.upstreamPath || ''}`);

if (options.record.preserveSearch) {
upstreamUrl.search = new URL(options.request.url).search;
}

return upstreamUrl;
}

export async function handleRewrite(options: {
request: Request;
cacheStorageId: number;
Expand All @@ -114,7 +128,7 @@ export async function handleRewrite(options: {
match: string | null;
publicDomain: string;
}): Promise<Response> {
const url = `https://${options.record.rewrite}${options.upstreamPath || ''}`;
const url = buildUpstreamUrl(options).toString();
const cacheKey = new Request(url, options.request);
const cache = await caches.open(String(options.cacheStorageId));
let response = await cache.match(cacheKey);
Expand Down
Loading
Loading