Skip to content

Commit

Permalink
Add hashing of static chunks at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlinator committed Jan 22, 2022
1 parent 74a3432 commit a74901e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/kit/src/core/build/build_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { copy_assets, get_aliases } from '../utils.js';
import { create_build, find_deps } from './utils.js';
import { SVELTE_KIT } from '../constants.js';
import { posixify } from '../../utils/filesystem.js';
import { createHash } from 'crypto';

/**
* @param {{
Expand Down Expand Up @@ -108,6 +109,10 @@ export async function build_client({
return {
assets,
chunks,
hashes: chunks.reduce((/** @type {Object.<string, string>} */ obj, item) => {
obj[item.fileName] = createHash('sha256').update(item.code).digest('base64');
return obj;
}, {}),
entry: {
file: vite_manifest[entry].file,
js: Array.from(entry_js),
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/generate_manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function generate_manifest(
return `{
appDir: ${s(build_data.app_dir)},
assets: new Set(${s(build_data.manifest_data.assets.map(asset => asset.file))}),
hashes: ${s(build_data.client.hashes)},
_: {
mime: ${s(get_mime_lookup(build_data.manifest_data))},
entry: ${s(build_data.client.entry)},
Expand Down
7 changes: 6 additions & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ export async function render_response({

if (page_config.router || page_config.hydrate) {
head += Array.from(js)
.map((dep) => `\n\t<link rel="modulepreload" href="${options.prefix + dep}">`)
.map(
(dep) =>
`\n\t<link rel="modulepreload" href="${options.prefix + dep}" integrity="sha256-${
options.manifest.hashes?.[dep]
}">`
)
.join('');
// prettier-ignore
head += `
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class InternalApp extends App {
export interface SSRManifest {
appDir: string;
assets: Set<string>;
hashes?: { [fileName: string]: string };
/** private fields */
_: {
mime: Record<string, string>;
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export interface BuildData {
client: {
assets: OutputAsset[];
chunks: OutputChunk[];
hashes: { [fileName: string]: string };
entry: {
file: string;
js: string[];
Expand Down

0 comments on commit a74901e

Please sign in to comment.