Skip to content

Commit

Permalink
Final pieces for prod
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Sep 9, 2022
1 parent ffd3266 commit 3ef723b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/pages-shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cloudflare/pages-shared",
"version": "0.0.3",
"version": "0.0.4",
"files": [
"tsconfig.json",
"src/**/*",
Expand Down
20 changes: 8 additions & 12 deletions packages/pages-shared/src/asset-server/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import "../environment-polyfills/types";

import {
FoundResponse,
InternalServerErrorResponse,
Expand Down Expand Up @@ -87,31 +85,30 @@ type FullHandlerContext<AssetEntry, ContentNegotiation, Asset> = {
attachAdditionalHeaders?: (
response: Response,
content: ContentNegotiation,
assetEntry: AssetEntry
assetEntry: AssetEntry,
asset: Asset
) => void;
caches: CacheStorage;
waitUntil: (promise: Promise<unknown>) => void;
HTMLRewriter: typeof HTMLRewriter;
};

export type HandlerContext<AssetEntry, ContentNegotiation, Asset> =
| FullHandlerContext<AssetEntry, ContentNegotiation, Asset>
| (Omit<
FullHandlerContext<AssetEntry, ContentNegotiation, Asset>,
"caches" | "waitUntil" | "HTMLRewriter"
"caches" | "waitUntil"
> & {
caches?: undefined;
waitUntil?: undefined;
HTMLRewriter?: undefined;
});

export async function generateHandler<
AssetEntry,
ContentNegotiation extends { encoding: string | null } = {
encoding: string | null;
},
Asset extends { body: ReadableStream; contentType: string } = {
body: ReadableStream;
Asset extends { body: ReadableStream | null; contentType: string } = {
body: ReadableStream | null;
contentType: string;
}
>({
Expand Down Expand Up @@ -139,7 +136,6 @@ export async function generateHandler<
attachAdditionalHeaders = () => {},
caches,
waitUntil,
HTMLRewriter: Rewriter,
}: HandlerContext<AssetEntry, ContentNegotiation, Asset>) {
const url = new URL(request.url);
const { protocol, host, search } = url;
Expand Down Expand Up @@ -358,7 +354,7 @@ export async function generateHandler<
try {
const links: { href: string; rel: string; as?: string }[] = [];

const transformedResponse = new Rewriter()
const transformedResponse = new HTMLRewriter()
.on("link[rel=preconnect],link[rel=preload]", {
element(element) {
const href = element.getAttribute("href") || undefined;
Expand Down Expand Up @@ -468,7 +464,7 @@ export async function generateHandler<
response.headers.append("cache-control", CACHE_CONTROL_BROWSER);
}

attachAdditionalHeaders(response, content, servingAssetEntry);
attachAdditionalHeaders(response, content, servingAssetEntry, asset);

if (isPreview(new URL(request.url))) {
response.headers.set("x-robots-tag", "noindex");
Expand All @@ -488,7 +484,7 @@ export async function generateHandler<
);
preservedResponse.headers.set("x-robots-tag", "noindex");

if (waitUntil) {
if (waitUntil && caches) {
waitUntil(
caches
.open(ASSET_PRESERVATION_CACHE)
Expand Down
2 changes: 0 additions & 2 deletions packages/pages-shared/src/asset-server/responses.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import "../environment-polyfills/types";

type HeadersInit = ConstructorParameters<typeof Headers>[0];

function mergeHeaders(base: HeadersInit, extra: HeadersInit) {
Expand Down
2 changes: 0 additions & 2 deletions packages/pages-shared/src/asset-server/rulesEngine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import "../environment-polyfills/types";

// Taken from https://stackoverflow.com/a/3561711
// which is everything from the tc39 proposal, plus the following two characters: ^/
// It's also everything included in the URLPattern escape (https://wicg.github.io/urlpattern/#escape-a-regexp-string), plus the following: -
Expand Down

0 comments on commit 3ef723b

Please sign in to comment.