Skip to content
Closed
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
21 changes: 3 additions & 18 deletions apps/svelte.dev/src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,9 @@ const fonts = [

/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) {
// Best effort to redirect from Svelte 4 docs to new docs
if (event.url.pathname.startsWith('/docs')) {
const destination = mappings.get(event.url.pathname);
if (destination) {
redirect(307, destination); // TODO make 301 once we're sure
}
if (!event.url.pathname.startsWith('/playground')) {
redirect(301, 'https://svelte.dev/' + event.url.pathname);
}

const response = await resolve(event, {
preload: ({ type, path }) => {
if (type === 'font') {
if (!path.endsWith('.woff2')) return false;
return fonts.some((font) => path.includes(font));
}

return type === 'js' || type === 'css'; // future-proof, if we add `assets` later
}
});

return response;
return resolve(event);
}
12 changes: 0 additions & 12 deletions apps/svelte.dev/src/routes/(authed)/+layout.server.js

This file was deleted.

27 changes: 0 additions & 27 deletions apps/svelte.dev/src/routes/(authed)/+layout.svelte

This file was deleted.

20 changes: 8 additions & 12 deletions apps/svelte.dev/src/routes/(authed)/playground/[id]/+page.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { browser } from '$app/environment';
import { redirect } from '@sveltejs/kit';

export function load({ data, url }) {
// initialize vim with the search param
const vim_search_params = url.searchParams.get('vim');
let vim = vim_search_params !== null && vim_search_params !== 'false';
// when in the browser check if there's a local storage entry and eventually override
// vim if there's not a search params otherwise update the local storage
export function load() {
// redirect old svelte-5-preview.vercel.app playground links,
// which all have a hash that starts with this pattern
if (browser) {
const vim_local_storage = window.localStorage.getItem('svelte:vim-enabled');
if (vim_search_params !== null) {
window.localStorage.setItem('svelte:vim-enabled', vim.toString());
} else if (vim_local_storage) {
vim = vim_local_storage !== 'false';
if (location.hash) {
redirect(307, `https://svelte.dev/${location.pathname}#${location.hash}`);
} else {
redirect(307, `https://svelte.dev/${location.pathname}`);
}
}
return { ...data, vim };
}

This file was deleted.

223 changes: 0 additions & 223 deletions apps/svelte.dev/src/routes/(authed)/playground/[id]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,223 +0,0 @@
<script lang="ts">
import { browser } from '$app/environment';
import { afterNavigate, goto, replaceState } from '$app/navigation';
import type { Gist } from '$lib/db/types';
import { Repl } from '@sveltejs/repl';
import { theme } from '@sveltejs/site-kit/stores';
import { onMount } from 'svelte';
import { mapbox_setup } from '../../../../config.js';
import AppControls from './AppControls.svelte';
import { compress_and_encode_text, decode_and_decompress_text } from './gzip.js';
import { page } from '$app/stores';
import type { File } from 'editor';

let { data } = $props();

let repl = $state() as ReturnType<typeof Repl>;
let name = $state(data.gist.name);
let modified = $state(false);
let version = data.version;
let setting_hash: any = null;

// Hashed URLs are less safe (we can't delete malicious REPLs), therefore
// don't allow links to escape the sandbox restrictions
const can_escape = browser && !$page.url.hash;

onMount(() => {
if (version !== 'local') {
fetch(`https://unpkg.com/svelte@${version}/package.json`)
.then((r) => r.json())
.then((pkg) => {
if (pkg.version !== version) {
version = pkg.version;

let url = `/playground/${data.gist.id}?version=${version}`;
if (location.hash) {
url += location.hash;
}
replaceState(url, {});
}
});
}
});

afterNavigate(() => {
name = data.gist.name;
set_files();
});

// TODO make this munging unnecessary
function munge(data: any): File {
const basename = `${data.name}.${data.type}`;

return {
type: 'file',
name: basename,
basename,
contents: data.source,
text: true
};
}

async function set_files() {
const hash = location.hash.slice(1);

if (!hash) {
repl?.set({
// TODO make this munging unnecessary
files: structuredClone(data.gist.components).map(munge)
});

modified = false;
return;
}

try {
let files = JSON.parse(await decode_and_decompress_text(hash)).files;

if (files[0]?.source) {
files = files.map(munge);
}

repl.set({ files });
} catch {
alert(`Couldn't load the code from the URL. Make sure you copied the link correctly.`);
}
}

function handle_fork({ gist }: { gist: Gist }) {
goto(`/playground/${gist.id}?version=${version}`);
}

function handle_save() {
// Hide hash from URL
const hash = location.hash.slice(1);
if (hash) {
change_hash();
}
}

async function change_hash(hash?: string) {
let url = `${location.pathname}${location.search}`;
if (hash) {
url += `#${await compress_and_encode_text(hash)}`;
}

clearTimeout(setting_hash);
replaceState(url, {});
setting_hash = setTimeout(() => {
setting_hash = null;
}, 500);
}

function onchange() {
const was_modified = modified;
modified = true;

if (
!was_modified &&
modified &&
name === data.gist.name &&
data.examples.some((section) =>
section.examples.some((example) => example.slug === data.gist.id)
)
) {
name = `${name} (edited)`;
}
}

const svelteUrl =
browser && version === 'local'
? `${location.origin}/playground/local`
: `https://unpkg.com/svelte@${version}`;

const relaxed = $derived(data.gist.relaxed || (data.user && data.user.id === data.gist.owner));
</script>

<svelte:head>
<title>{name} • Playground • Svelte</title>

<meta name="twitter:title" content="{data.gist.name} • Playground • Svelte" />
<meta name="twitter:description" content="Web development, but fun" />
<meta name="Description" content="Interactive Svelte playground" />
</svelte:head>

<svelte:window
on:hashchange={() => {
if (!setting_hash) {
set_files();
}
}}
/>

<div class="repl-outer">
<AppControls
examples={data.examples}
user={data.user}
gist={data.gist}
forked={handle_fork}
saved={handle_save}
{repl}
bind:name
bind:modified
/>

{#if browser}
<div
style="display: contents"
onfocusout={() => {
// Only change hash on editor blur to not pollute everyone's browser history
if (modified) {
const json = JSON.stringify({ files: repl.toJSON().files });
change_hash(json);
}
}}
>
<Repl
bind:this={repl}
{svelteUrl}
{relaxed}
{can_escape}
injectedJS={mapbox_setup}
{onchange}
previewTheme={$theme.current}
/>
</div>
{/if}
</div>

<style>
.repl-outer {
position: relative;
height: calc(100% - var(--sk-nav-height) - var(--sk-banner-bottom-height));
height: calc(100dvh - var(--sk-nav-height) - var(--sk-banner-bottom-height));
overflow: hidden;
background-color: var(--sk-back-1);
box-sizing: border-box;
display: flex;
flex-direction: column;
}

/* temp fix for #2499 and #2550 while waiting for a fix for https://github.com/sveltejs/svelte-repl/issues/8 */

.repl-outer :global(.tab-content),
.repl-outer :global(.tab-content.visible) {
pointer-events: all;
opacity: 1;
}
.repl-outer :global(.tab-content) {
visibility: hidden;
}
.repl-outer :global(.tab-content.visible) {
visibility: visible;
}

@keyframes fade-in {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export type Examples = Array<{
}>;
}>;

export const prerender = true;

async function munge(files: Record<string, string>) {
const result = [];

Expand Down
Loading
Loading