Skip to content
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

Disallow /index.html in sandboxes to fix forks on CodeSandbox #7430

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading