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
5 changes: 5 additions & 0 deletions website/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { resolve } from "pathe";
import rehypeMermaid from "rehype-mermaid";
import remarkHeadingID from "remark-heading-id";
import current from "./src/content/current-sidebar";
import { computeSriHash } from "./src/utils/sri-hash";

/** Scan the release-notes directory and return the slug of the latest release note. */
function getLatestReleaseNoteSlug() {
Expand Down Expand Up @@ -43,6 +44,8 @@ const latestReleaseNote = getLatestReleaseNoteSlug();

const base = process.env.TYPESPEC_WEBSITE_BASE_PATH ?? "/";

const initJsIntegrity = computeSriHash("1ds-init.js");

// https://astro.build/config
export default defineConfig({
base,
Expand Down Expand Up @@ -98,6 +101,7 @@ export default defineConfig({
tag: "script",
attrs: {
src: "https://consentdeliveryfd.azurefd.net/mscc/lib/v2/wcp-consent.js",
crossorigin: "anonymous",
},
},
{
Expand All @@ -106,6 +110,7 @@ export default defineConfig({
type: "module",
async: true,
src: "1ds-init.js",
integrity: initJsIntegrity,
},
},
],
Expand Down
10 changes: 8 additions & 2 deletions website/src/layouts/base-layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import "@site/src/css/custom.css";
import Header from "@site/src/components/header/header.astro";
import Footer from "@site/src/components/footer/footer.astro";
import { baseUrl } from "@typespec/astro-utils/utils/base-url";
import { computeSriHash } from "@site/src/utils/sri-hash";

export interface Props {
/** Whether to render the footer @default true */
footer?: boolean;
}
const { footer = true } = Astro.props;

const initJsIntegrity = computeSriHash("1ds-init.js");
---

<html lang="en">
Expand All @@ -18,9 +21,12 @@ const { footer = true } = Astro.props;
<link rel="og:image" type="image/svg+xml" href={baseUrl("/img/social.svg")} />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>typespec.io</title>
<script src="https://consentdeliveryfd.azurefd.net/mscc/lib/v2/wcp-consent.js" is:inline
<script
src="https://consentdeliveryfd.azurefd.net/mscc/lib/v2/wcp-consent.js"
crossorigin="anonymous"
is:inline></script>
<script src={import.meta.env.BASE_URL + "1ds-init.js"} integrity={initJsIntegrity} is:inline
></script>
<script src={import.meta.env.BASE_URL + "1ds-init.js"} is:inline></script>
</head>
<body class="body">
<header class="header">
Expand Down
16 changes: 16 additions & 0 deletions website/src/utils/sri-hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";

/**
* Compute a SHA-384 subresource integrity (SRI) hash for the given file,
* relative to the website `public/` directory.
*
* Uses `process.cwd()` because Astro always runs from the website root,
* and `import.meta.dirname` is unreliable after bundling during pre-render.
*/
export function computeSriHash(publicRelativePath: string): string {
const absPath = resolve(process.cwd(), "public", publicRelativePath);
const content = readFileSync(absPath);
return `sha384-${createHash("sha384").update(content).digest("base64")}`;
}
Loading