Skip to content

Commit 7202180

Browse files
Fix StackBlitz embed isolation (#945)
1 parent e969361 commit 7202180

11 files changed

Lines changed: 49 additions & 7 deletions

src/components/InteractiveSandbox.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react'
2+
import { stackBlitzIframeProps } from '~/utils/stackblitz-embed'
23

34
interface InteractiveSandboxProps {
45
isActive: boolean
@@ -17,6 +18,8 @@ export function InteractiveSandbox({
1718
libraryName,
1819
embedEditor,
1920
}: InteractiveSandboxProps) {
21+
const isStackBlitz = embedEditor === 'stackblitz'
22+
2023
return (
2124
<div
2225
className={`absolute inset-0 ${
@@ -25,8 +28,9 @@ export function InteractiveSandbox({
2528
aria-hidden={!isActive}
2629
>
2730
<iframe
28-
src={embedEditor === 'codesandbox' ? codeSandboxUrl : stackBlitzUrl}
31+
src={isStackBlitz ? stackBlitzUrl : codeSandboxUrl}
2932
title={`${libraryName} | ${examplePath}`}
33+
{...(isStackBlitz ? stackBlitzIframeProps : {})}
3034
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
3135
className="w-full h-full min-h-[80dvh] overflow-hidden shadow-lg bg-white dark:bg-black"
3236
/>

src/components/StackBlitzEmbed.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react'
22
import { useIsDark } from '~/hooks/useIsDark'
3+
import { stackBlitzIframeProps } from '~/utils/stackblitz-embed'
34

45
type StackBlitzEmbedProps = {
56
repo: string
@@ -32,6 +33,7 @@ export function StackBlitzEmbed({
3233
title={title || `${repo}: ${examplePath}`}
3334
key={`${examplePath}-${themeParam}`}
3435
src={src}
36+
{...stackBlitzIframeProps}
3537
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
3638
className="shadow-lg"
3739
loading="lazy"

src/routes/$libraryId/$version.docs.framework.$framework.examples.$.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
ExampleDeployDialog,
2525
type DeployProvider,
2626
} from '~/components/ExampleDeployDialog'
27+
import { stackBlitzEmbedHeaders } from '~/utils/stackblitz-embed'
2728

2829
const renderedFileQueryOptions = (
2930
repo: string,
@@ -136,6 +137,7 @@ export const Route = createFileRoute(
136137
}),
137138
}
138139
},
140+
headers: () => stackBlitzEmbedHeaders,
139141
staleTime: 1000 * 60 * 5, // 5 minutes
140142
})
141143

src/routes/-library-landing.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import { getTanstackDocsConfig } from '~/utils/config'
99
import { fetchLandingCodeExample } from '~/utils/landing-code-example.functions'
1010
import { seo } from '~/utils/seo'
1111
import { ogImageUrl } from '~/utils/og'
12+
import { stackBlitzEmbedHeaders } from '~/utils/stackblitz-embed'
1213

1314
export type LandingComponentProps = {
1415
landingCodeExampleRsc?: ReactNode
1516
}
1617

1718
type LandingComponent = ComponentType<LandingComponentProps>
1819

20+
type LibraryLandingPageOptions = {
21+
hasStackBlitzEmbed?: boolean
22+
}
23+
1924
type StaticLandingRoutePath =
2025
| '/ai/$version/'
2126
| '/cli/$version/'
@@ -108,6 +113,7 @@ export function createLibraryLandingPage<TId extends StaticLandingRoutePath>(
108113
routePath: TId,
109114
libraryId: LibraryId,
110115
LandingComponent: LandingComponent,
116+
options: LibraryLandingPageOptions = {},
111117
) {
112118
const library = getLibrary(libraryId)
113119
const routeApi = getRouteApi(routePath)
@@ -181,6 +187,9 @@ export function createLibraryLandingPage<TId extends StaticLandingRoutePath>(
181187
noindex: library.visible === false,
182188
}),
183189
}),
190+
...(options.hasStackBlitzEmbed
191+
? { headers: () => stackBlitzEmbedHeaders }
192+
: {}),
184193
staticData: {
185194
Title: () => (
186195
<LibraryNavbarTitle libraryId={libraryId} routePath={routePath} />

src/routes/form.$version.index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ import FormLanding from '~/components/landing/FormLanding'
33
import { createLibraryLandingPage } from './-library-landing'
44

55
export const Route = createFileRoute('/form/$version/')(
6-
createLibraryLandingPage('/form/$version/', 'form', FormLanding),
6+
createLibraryLandingPage('/form/$version/', 'form', FormLanding, {
7+
hasStackBlitzEmbed: true,
8+
}),
79
)

src/routes/query.$version.index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ import QueryLanding from '~/components/landing/QueryLanding'
33
import { createLibraryLandingPage } from './-library-landing'
44

55
export const Route = createFileRoute('/query/$version/')(
6-
createLibraryLandingPage('/query/$version/', 'query', QueryLanding),
6+
createLibraryLandingPage('/query/$version/', 'query', QueryLanding, {
7+
hasStackBlitzEmbed: true,
8+
}),
79
)

src/routes/ranger.$version.index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ import RangerLanding from '~/components/landing/RangerLanding'
33
import { createLibraryLandingPage } from './-library-landing'
44

55
export const Route = createFileRoute('/ranger/$version/')(
6-
createLibraryLandingPage('/ranger/$version/', 'ranger', RangerLanding),
6+
createLibraryLandingPage('/ranger/$version/', 'ranger', RangerLanding, {
7+
hasStackBlitzEmbed: true,
8+
}),
79
)

src/routes/router.$version.index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ import RouterLanding from '~/components/landing/RouterLanding'
33
import { createLibraryLandingPage } from './-library-landing'
44

55
export const Route = createFileRoute('/router/$version/')(
6-
createLibraryLandingPage('/router/$version/', 'router', RouterLanding),
6+
createLibraryLandingPage('/router/$version/', 'router', RouterLanding, {
7+
hasStackBlitzEmbed: true,
8+
}),
79
)

src/routes/table.$version.index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ import TableLanding from '~/components/landing/TableLanding'
33
import { createLibraryLandingPage } from './-library-landing'
44

55
export const Route = createFileRoute('/table/$version/')(
6-
createLibraryLandingPage('/table/$version/', 'table', TableLanding),
6+
createLibraryLandingPage('/table/$version/', 'table', TableLanding, {
7+
hasStackBlitzEmbed: true,
8+
}),
79
)

src/routes/virtual.$version.index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ import VirtualLanding from '~/components/landing/VirtualLanding'
33
import { createLibraryLandingPage } from './-library-landing'
44

55
export const Route = createFileRoute('/virtual/$version/')(
6-
createLibraryLandingPage('/virtual/$version/', 'virtual', VirtualLanding),
6+
createLibraryLandingPage('/virtual/$version/', 'virtual', VirtualLanding, {
7+
hasStackBlitzEmbed: true,
8+
}),
79
)

0 commit comments

Comments
 (0)