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: 4 additions & 1 deletion apps/cli-docs/src/components/PageFrame.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import Default from "@astrojs/starlight/components/PageFrame.astro";
import Analytics from "@vercel/analytics/astro";
import SpeedInsights from "@vercel/speed-insights/astro";

import RedirectBanner from "./RedirectBanner.astro";
---

<!-- Starlight owns the layout; this wrapper preserves its slots and only mounts global telemetry. -->
<!-- Starlight owns the layout; this wrapper preserves its slots and only mounts global telemetry and the redirect banner. -->
<Default>
<slot />
<slot name="header" slot="header" />
Expand All @@ -13,3 +15,4 @@ import SpeedInsights from "@vercel/speed-insights/astro";

<Analytics />
<SpeedInsights />
<RedirectBanner />
42 changes: 42 additions & 0 deletions apps/cli-docs/src/components/RedirectBanner.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
const baseUrl = import.meta.env.BASE_URL;
const base = baseUrl.endsWith("/") ? baseUrl : baseUrl + "/";
---

<!--
The docs.sentry.io /cli/* redirects add ?from=sentry-docs. Only then does the script put the
banner above <main>, outside it, so the theme's `main > .content-panel:first-child` spacing holds.
-->
<template id="redirect-banner">
<div class="redirect-banner" data-pagefind-ignore>
Sentry CLI docs have moved here. Coming from <code>sentry-cli</code> v3? Read the{" "}
<a href={`${base}migrating-from-v3/`}>migration guide</a>. Looking for v3? Its ghost haunts{" "}
<a href="https://github.com/getsentry/sentry-cli">getsentry/sentry-cli</a>.
</div>
</template>
<script is:inline>
(function () {
var url = new URL(window.location.href);
if (url.searchParams.get("from") !== "sentry-docs") return;
document.querySelector("main").before(document.getElementById("redirect-banner").content);
url.searchParams.delete("from");
history.replaceState(history.state, "", url.pathname + url.search + url.hash);
})();
</script>

<style>
/* Same layout as Starlight's banner, in the CLI brand purple. */
.redirect-banner {
padding: var(--sl-nav-pad-y) var(--sl-nav-pad-x);
background-color: rgb(var(--sl-color-accent-rgb));
color: #fff;
line-height: var(--sl-line-height-headings);
text-align: center;
text-wrap: balance;
box-shadow: var(--sl-shadow-sm);
}

.redirect-banner a {
color: inherit;
}
</style>
38 changes: 38 additions & 0 deletions apps/cli-docs/test/redirect-banner.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { runInNewContext } from "node:vm";
import test from "node:test";

const component = readFileSync(fileURLToPath(new URL("../src/components/RedirectBanner.astro", import.meta.url)), "utf8");
const script = component.match(/<script is:inline>([\s\S]*?)<\/script>/)?.[1];
assert.ok(script, "RedirectBanner must include its inline script");

function load(href) {
const inserted = [];
const replaced = [];
const banner = { content: "banner" };
const document = {
querySelector: (selector) => (selector === "main" ? { before: (node) => inserted.push(node) } : null),
getElementById: (id) => (id === "redirect-banner" ? banner : null),
};
const history = { state: { page: 1 }, replaceState: (...args) => replaced.push(args) };
runInNewContext(script, { URL, document, history, window: { location: { href } } });
return { inserted, replaced };
}

test("a docs.sentry.io redirect shows the banner and drops only its param from the address", () => {
const { inserted, replaced } = load("https://cli.sentry.dev/getting-started/?from=sentry-docs&x=1#install-script");

assert.deepEqual(inserted, ["banner"]);
assert.deepEqual(replaced, [[{ page: 1 }, "", "/getting-started/?x=1#install-script"]]);
});

test("other visits leave the page and the address alone", () => {
for (const href of ["https://cli.sentry.dev/getting-started/", "https://cli.sentry.dev/?from=elsewhere"]) {
const { inserted, replaced } = load(href);

assert.deepEqual(inserted, [], href);
assert.deepEqual(replaced, [], href);
}
});
Loading