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

allow custom cache control settings for requests #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions src/server/publisher-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function requestAcceptsTextHtml(req: Request) {
type AssetInit = {
status?: number,
headers?: Record<string, string>,
cache?: 'extended' | 'never' | null,
cache?: 'extended' | 'never' | null | string,
};

export class PublisherServer {
Expand Down Expand Up @@ -238,6 +238,8 @@ export class PublisherServer {
case 'never':
cacheControlValue = 'no-store';
break;
default:
cacheControlValue = init.cache;
}
headers['Cache-Control'] = cacheControlValue;
}
Expand All @@ -264,7 +266,7 @@ export class PublisherServer {
});
}

async serveRequest(request: Request): Promise<Response | null> {
async serveRequest(request: Request, cache: string): Promise<Response | null> {

// Only handle GET and HEAD
if (request.method !== 'GET' && request.method !== 'HEAD') {
Expand All @@ -274,10 +276,12 @@ export class PublisherServer {
const url = new URL(request.url);
const pathname = url.pathname;

const doCache = cache || (this.testExtendedCache(pathname) ? 'extended' : null);

const asset = this.getMatchingAsset(pathname);
if (asset != null) {
return this.serveAsset(request, asset, {
cache: this.testExtendedCache(pathname) ? 'extended' : null,
cache: doCache,
});
}

Expand Down