Skip to content

Commit

Permalink
fix: add polyfills / avoid using structuredClone (sveltejs#915)
Browse files Browse the repository at this point in the history
- polyfills for `at` and `withResolvers`
- avoid using `structuredClone`, we can use `JSON.parse(JSON.stringify(..))` instead at that position

makes the site work in more browsers

closes sveltejs#911
  • Loading branch information
dummdidumm authored Dec 3, 2024
1 parent c8f62af commit 0d2d24f
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions apps/svelte.dev/src/hooks.client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@sveltejs/site-kit/polyfills';
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
if (!hash && !saved) {
repl?.set({
// TODO make this munging unnecessary
files: structuredClone(data.gist.components).map(munge)
// TODO make this munging unnecessary (using JSON instead of structuredClone for better browser compat)
files: JSON.parse(JSON.stringify(data.gist.components)).map(munge)
});
modified = false;
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/lib/compile-worker/worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@sveltejs/site-kit/polyfills';
import { parseTar } from 'tarparser';
import type { CompileResult } from 'svelte/compiler';
import type { ExposedCompilerOptions, File } from '../Workspace.svelte';
Expand Down
1 change: 1 addition & 0 deletions packages/repl/src/lib/workers/bundler/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@sveltejs/site-kit/polyfills';
import '../patch_window';
import { sleep } from '../../utils';
import { rollup } from '@rollup/browser';
Expand Down
3 changes: 3 additions & 0 deletions packages/site-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
"default": "./src/lib/nav/index.ts",
"svelte": "./src/lib/nav/index.ts"
},
"./polyfills": {
"default": "./src/lib/polyfills/index.ts"
},
"./icons/link.svg": "./src/lib/icons/link.svg",
"./icons/search.svg": "./src/lib/icons/search.svg",
"./search": {
Expand Down
18 changes: 18 additions & 0 deletions packages/site-kit/src/lib/polyfills/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Some polyfills for things used throughout the app for better browser compat

if (!Array.prototype.at) {
Array.prototype.at = /** @param {number} index */ function (index) {
return this[index >= 0 ? index : this.length + index];
};
}

if (!Promise.withResolvers) {
Promise.withResolvers = function () {
let resolve: any, reject: any;
const promise = new Promise<any>((res, rej) => {
resolve = res;
reject = rej;
});
return { resolve, reject, promise };
};
}
1 change: 1 addition & 0 deletions packages/site-kit/src/lib/search/search.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@sveltejs/site-kit/polyfills';
import flexsearch, { type Index as FlexSearchIndex } from 'flexsearch';
import type { Block, BlockGroup } from './types';

Expand Down

0 comments on commit 0d2d24f

Please sign in to comment.