Disallow /index.html in sandboxes (#7430)

This commit is contained in:
Sebastian "Sebbie" Silbermann
2025-01-14 20:29:35 +01:00
committed by GitHub
parent 316230adfc
commit ee8a829f99
6 changed files with 19 additions and 11 deletions

View File

@@ -71,6 +71,13 @@ function SandpackRoot(props: SandpackProps) {
const codeSnippets = Children.toArray(children) as React.ReactElement[];
const files = createFileMap(codeSnippets);
if ('/index.html' in files) {
throw new Error(
'You cannot use `index.html` file in sandboxes. ' +
'Only `public/index.html` is respected by Sandpack and CodeSandbox (where forks are created).'
);
}
files['/src/styles.css'] = {
code: [sandboxStyle, files['/src/styles.css']?.code ?? ''].join('\n\n'),
hidden: !files['/src/styles.css']?.visible,

View File

@@ -57,12 +57,13 @@ Then add these lines of code at the top of your main JavaScript file (it might b
<Sandpack>
```html index.html hidden
```html public/index.html hidden
<!DOCTYPE html>
<html>
<head><title>My app</title></head>
<body>
<!-- Your existing page content (in this example, it gets replaced) -->
<div id="root"></div>
</body>
</html>
```
@@ -119,7 +120,7 @@ This lets you find that HTML element with [`document.getElementById`](https://de
<Sandpack>
```html index.html
```html public/index.html
<!DOCTYPE html>
<html>
<head><title>My app</title></head>

View File

@@ -144,7 +144,7 @@ Usually, you only need to run this code once at startup. It will:
<Sandpack>
```html index.html
```html public/index.html
<!DOCTYPE html>
<html>
<head><title>My app</title></head>
@@ -375,7 +375,7 @@ You can use the `onUncaughtError` root option to display error dialogs:
<Sandpack>
```html index.html hidden
```html public/index.html hidden
<!DOCTYPE html>
<html>
<head>
@@ -606,7 +606,7 @@ You can use the `onCaughtError` root option to display error dialogs or filter k
<Sandpack>
```html index.html hidden
```html public/index.html hidden
<!DOCTYPE html>
<html>
<head>
@@ -885,7 +885,7 @@ You can use the `onRecoverableError` root option to display error dialogs:
<Sandpack>
```html index.html hidden
```html public/index.html hidden
<!DOCTYPE html>
<html>
<head>

View File

@@ -406,7 +406,7 @@ You can use the `onUncaughtError` root option to display error dialogs:
<Sandpack>
```html index.html hidden
```html public/index.html hidden
<!DOCTYPE html>
<html>
<head>
@@ -641,7 +641,7 @@ You can use the `onCaughtError` root option to display error dialogs or filter k
<Sandpack>
```html index.html hidden
```html public/index.html hidden
<!DOCTYPE html>
<html>
<head>
@@ -922,7 +922,7 @@ You can use the `onRecoverableError` root option to display error dialogs for hy
<Sandpack>
```html index.html hidden
```html public/index.html hidden
<!DOCTYPE html>
<html>
<head>

View File

@@ -252,7 +252,7 @@ Portals can be useful if your React root is only part of a static or server-rend
<Sandpack>
```html index.html
```html public/index.html
<!DOCTYPE html>
<html>
<head><title>My app</title></head>

View File

@@ -226,7 +226,7 @@ If you render multiple independent React applications on a single page, pass `id
<Sandpack>
```html index.html
```html public/index.html
<!DOCTYPE html>
<html>
<head><title>My app</title></head>