Skip to content

Commit

Permalink
Merge pull request #1013 from MuckRock/1008-detect-iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast authored Jan 16, 2025
2 parents 252f17e + b8cb148 commit 338ec4b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/lib/components/onboarding/GuidedTour.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
</script>

<script lang="ts">
import { afterNavigate } from "$app/navigation";
import { onMount } from "svelte";
import { getCurrentUser } from "$lib/utils/permissions";
Expand All @@ -85,6 +86,11 @@
}
}
});
afterNavigate(() => {
// if we navigate anywhere else, end the tour
endTour();
});
</script>

<slot />
Expand Down
13 changes: 11 additions & 2 deletions src/routes/(app)/documents/[id]-[slug]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ import type { Access, Document, Note } from "$lib/api/types";
import { fail } from "@sveltejs/kit";
import { setFlash, redirect } from "sveltekit-flash-message/server";

import { CSRF_COOKIE_NAME } from "@/config/config.js";
import { CSRF_COOKIE_NAME, EMBED_URL } from "@/config/config.js";
import { destroy, edit, redact } from "$lib/api/documents";
import * as notes from "$lib/api/notes";
import { isErrorCode } from "$lib/utils/api";

export function load({ cookies }) {
export function load({ cookies, request, url }) {
const inIframe = request.headers.get("Sec-Fetch-Dest") === "iframe";
if (inIframe) {
const embed = new URL(url);

embed.host = new URL(EMBED_URL).host;
embed.searchParams.set("embed", "1");
return redirect(307, embed);
}

const csrf_token = cookies.get(CSRF_COOKIE_NAME);

return { csrf_token };
Expand Down
14 changes: 12 additions & 2 deletions src/routes/(app)/documents/[id]-[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
-->

<script lang="ts">
import "@/style/kit.css";
import { browser } from "$app/environment";
import { goto } from "$app/navigation";
import { page } from "$app/stores";
import { onMount } from "svelte";
import { embedUrl } from "$lib/api/embed";
import * as documents from "$lib/api/documents";
Expand All @@ -26,6 +27,15 @@
$: action = data.action;
$: hasDescription = Boolean(document.description?.trim().length);
onMount(() => {
// if we're in an iframe, redirect to the embed route
const inIframe = window.self !== window.top;
if (inIframe) {
const embedUrl = documents.embedUrl(document, $page.url.searchParams);
goto(embedUrl);
}
});
</script>

<svelte:head>
Expand Down
2 changes: 2 additions & 0 deletions src/routes/(app)/documents/[id]-[slug]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function load({
depends,
url,
setHeaders,
data,
}) {
const { document, asset_url, mode } = await loadDocument({
fetch,
Expand Down Expand Up @@ -55,6 +56,7 @@ export async function load({
}

return {
...data,
document,
mode,
asset_url,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/embed/+layout.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const trailingSlash = "always";
export const trailingSlash = "ignore";

0 comments on commit 338ec4b

Please sign in to comment.