Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Support crossorigin setting in push-manifest #85

Merged
merged 1 commit into from
Dec 6, 2018
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
- Support crossorigin setting in push-manifest.
<!-- Add new, unreleased changes here. -->

## [1.2.0] 2018-05-22
- Add `as=fetch` as a valid value for preload headers.

Expand Down
25 changes: 18 additions & 7 deletions src/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ import * as validUrl from 'valid-url';
* JSON format for a multi-file push manifest.
*/
export interface PushManifestData {
[pattern: string]: {[resource: string]: {type: string; weight?: number;}}
[pattern: string]: {
[resource: string]: {
type: string;
crossorigin?: string;
weight?: number;
}
}
}

/**
* Maps from an HTTP request path to the set of additional resources that
* should be pre-emptively pushed to the client via HTTP/2 server push.
*/
export class PushManifest {
private mapping = new Array<[RegExp, Map<string, {type: string}>]>();
private mapping =
new Array<[RegExp, Map<string, {type: string; crossorigin: string|null}>]>();

/**
* Create a new `PushManifest` from a JSON object which is expected to match
Expand Down Expand Up @@ -55,11 +62,12 @@ export class PushManifest {
const resources = new Map();
for (const resource of Object.keys(manifest[pattern])) {
validatePath(resource);
const t = manifest[pattern][resource].type || '';
if (!requestDestinations.has(t)) {
throw new Error(`invalid type: ${t}`);
const type = manifest[pattern][resource].type || '';
const crossorigin = manifest[pattern][resource].crossorigin || null;
if (!requestDestinations.has(type)) {
throw new Error(`invalid type: ${type}`);
}
resources.set(normalizePath(resource, basePath), {type: t});
resources.set(normalizePath(resource, basePath), {type, crossorigin});
}
if (resources.size) {
let normalizedPattern;
Expand Down Expand Up @@ -92,11 +100,14 @@ export class PushManifest {
if (!resources || !pattern.test(normalizedPattern)) {
continue;
}
for (const [resource, {type}] of resources.entries()) {
for (const [resource, {type, crossorigin}] of resources.entries()) {
let header = `<${resource}>; rel=preload`;
if (type) {
header += `; as=${type}`;
}
if (crossorigin) {
header += `; crossorigin=${crossorigin}`;
}
if (nopush) {
header += `; nopush`;
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/push_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,21 @@ suite('PushManifest', function() {
'</dep.html>; rel=preload; as=document; nopush',
]);
});

test('crossorigin setting works', () => {
const manifest = new push.PushManifest({
'/foo': {
'/a.html': {type: 'document'},
'/b.html': {type: 'document', crossorigin: ''},
'/c.html': {type: 'document', crossorigin: 'anonymous'},
'/d.html': {type: 'document', crossorigin: 'use-credentials'},
},
});
assert.deepEqual(manifest.linkHeaders('/foo'), [
'</a.html>; rel=preload; as=document',
'</b.html>; rel=preload; as=document',
'</c.html>; rel=preload; as=document; crossorigin=anonymous',
'</d.html>; rel=preload; as=document; crossorigin=use-credentials',
]);
});
});