-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cross-origin isolation flag #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{ | ||
"printWidth": 100, | ||
"singleQuote": true, | ||
"useTabs": false, | ||
"tabWidth": 2, | ||
"endOfLine": "lf", | ||
"semi": true, | ||
"arrowParens": "always", | ||
"bracketSpacing": true, | ||
"trailingComma": "es5" | ||
"printWidth": 100, | ||
"singleQuote": true, | ||
"useTabs": false, | ||
"tabWidth": 2, | ||
"endOfLine": "lf", | ||
"semi": true, | ||
"arrowParens": "always", | ||
"bracketSpacing": true, | ||
"trailingComma": "es5" | ||
} |
Nemikolh marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,8 @@ | |
<body> | ||
<h1>StackBlitz SDK Examples</h1> | ||
<ul> | ||
<li><a href="/examples/open-embed-project-id/">Open and embed a StackBlitz project</a></li> | ||
<li><a href="/examples/open-embed-project-id/">Open and embed a StackBlitz EngineBlock project</a></li> | ||
<li><a href="/examples/open-embed-webcontainer/">Open and embed a StackBlitz WebContainer project</a></li> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a new example to test loading WebContainer projects with and without the CORP headers. |
||
<li><a href="/examples/open-embed-github-project/">Open and embed a GitHub repo</a></li> | ||
<li><a href="/examples/embed-project-vm/">Control an embedded project with the SDK</a></li> | ||
</ul> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>StackBlitz SDK - Open and embed a StackBlitz WebContainer project</title> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<script type="module" src="./index.ts"></script> | ||
</head> | ||
<body> | ||
<header> | ||
<h1>Open and embed a StackBlitz WebContainer project</h1> | ||
<div> | ||
<label> | ||
<input type="checkbox" name="corp" /> Cross-Origin Isolation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking and unchecking this checkbox will load the page either with or without the CORP headers making it possible to test how embeds behave in different scenario's. |
||
</label> | ||
</div> | ||
<nav> | ||
<button type="button" name="embed-project">Embed project</button> | ||
<button type="button" name="open-project">Open project</button> | ||
</nav> | ||
</header> | ||
<div id="embed"> | ||
<p>Embed will go here</p> | ||
</div> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import sdk from '@stackblitz/sdk'; | ||
|
||
import './styles.css'; | ||
|
||
// This opens https://stackblitz.com/edit/node | ||
// in the current window with the Preview pane | ||
function openProject() { | ||
sdk.openProjectId('node', { | ||
newWindow: false, | ||
view: 'preview', | ||
}); | ||
} | ||
|
||
// This replaces the HTML element with | ||
// the id of "embed" with https://stackblitz.com/edit/node embedded in an iframe. | ||
function embedProject() { | ||
sdk.embedProjectId('embed', 'node', { | ||
openFile: 'index.ts', | ||
crossOriginIsolated: true, | ||
}); | ||
} | ||
|
||
function toggleCorp(event: Event) { | ||
const queryParams = new URLSearchParams(window.location.search); | ||
const isChecked = (event.target as any)?.checked; | ||
|
||
if (isChecked) { | ||
if (!queryParams.has('corp') || queryParams.get('corp') !== '1') { | ||
queryParams.set('corp', '1'); | ||
} | ||
} else { | ||
queryParams.delete('corp'); | ||
} | ||
|
||
window.location.search = queryParams.toString(); | ||
} | ||
|
||
function setup() { | ||
const embedButton = document.querySelector('[name=embed-project]') as HTMLButtonElement; | ||
const openButton = document.querySelector('[name=open-project]') as HTMLButtonElement; | ||
const corpCheckbox = document.querySelector('[name=corp]') as HTMLInputElement; | ||
|
||
embedButton.addEventListener('click', embedProject); | ||
openButton.addEventListener('click', openProject); | ||
corpCheckbox.addEventListener('change', toggleCorp); | ||
|
||
// mark the checkbox checked if the corp param is already set | ||
const queryParams = new URLSearchParams(window.location.search); | ||
|
||
corpCheckbox.checked = queryParams.get('corp') === '1'; | ||
} | ||
|
||
setup(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "sdk-example-open-embed-webcontainer", | ||
"description": "Demo of the StackBlitz SDK's methods for opening & embedding an existing StackBlitz WebContainer project", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"@stackblitz/sdk": "^1.8.1" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
html { | ||
height: 100%; | ||
text-align: center; | ||
font-family: system-ui, sans-serif; | ||
color: black; | ||
background-color: white; | ||
} | ||
|
||
body { | ||
height: 100%; | ||
margin: 0; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
h1 { | ||
margin: 1rem; | ||
font-size: 1.25rem; | ||
} | ||
|
||
nav { | ||
margin: 1rem; | ||
font-size: 0.9rem; | ||
} | ||
|
||
button { | ||
margin: 0.2em; | ||
padding: 0.2em 0.5em; | ||
font-size: inherit; | ||
font-family: inherit; | ||
} | ||
|
||
#embed { | ||
display: flex; | ||
flex: 1 1 60%; | ||
flex-direction: column; | ||
justify-content: center; | ||
overflow: hidden; | ||
width: 100%; | ||
height: auto; | ||
margin: 0; | ||
border: 0; | ||
} | ||
|
||
#embed > p { | ||
width: min(300px, 100%); | ||
margin: 2rem auto; | ||
padding: 4rem 1rem; | ||
border: dashed 2px #ccc; | ||
border-radius: 0.5em; | ||
font-size: 85%; | ||
color: #777; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ export function replaceAndEmbed( | |
frame.className = target.className; | ||
} | ||
setFrameDimensions(frame, options); | ||
setFrameAllowList(target, frame, options); | ||
target.replaceWith(frame); | ||
} | ||
|
||
|
@@ -81,3 +82,19 @@ function setFrameDimensions(frame: HTMLIFrameElement, options: EmbedOptions = {} | |
frame.setAttribute('style', 'width:100%;'); | ||
} | ||
} | ||
|
||
function setFrameAllowList( | ||
target: HTMLElement & { allow?: string }, | ||
frame: HTMLIFrameElement, | ||
options: EmbedOptions = {} | ||
) { | ||
const allowList = target.allow?.split(';')?.map((key) => key.trim()) ?? []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We copy the allow list which is already part of the iframe, and add |
||
|
||
if (options.crossOriginIsolated && !allowList.includes('cross-origin-isolated')) { | ||
allowList.push('cross-origin-isolated'); | ||
} | ||
|
||
if (allowList.length > 0) { | ||
frame.allow = allowList.join('; '); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran prettier and it fixed some stuff.