Skip to content

Commit

Permalink
Disallow /index.html in sandboxes (#7430)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Jan 14, 2025
1 parent 316230a commit ee8a829
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/components/MDX/Sandpack/SandpackRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/content/learn/add-react-to-an-existing-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
```
Expand Down Expand Up @@ -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>
Expand Down
8 changes: 4 additions & 4 deletions src/content/reference/react-dom/client/createRoot.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
6 changes: 3 additions & 3 deletions src/content/reference/react-dom/client/hydrateRoot.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/content/reference/react-dom/createPortal.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/content/reference/react/useId.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit ee8a829

Please sign in to comment.