Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit aa14e4b

Browse files
author
Je
committed
feat: add nomodule polyfill
1 parent 9b411a2 commit aa14e4b

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

aleph.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/// <reference lib="dom" />
21
import React, { ComponentType, useCallback, useEffect, useRef, useState } from 'https://esm.sh/react'
32
import { DataContext, RouterContext } from './context.ts'
43
import { E404Page, E501App, E501Page, ErrorBoundary } from './error.ts'
@@ -72,7 +71,7 @@ export function ALEPH({ initial }: {
7271
}, pageProps)
7372
setPage({ url, pageProps })
7473
if (util.isInt(e.scrollTo)) {
75-
window.scrollTo(e.scrollTo, 0)
74+
(window as any).scrollTo(e.scrollTo, 0)
7675
}
7776
} else {
7877
setPage({ url, pageProps: null })

html.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function createHtml({
99
}: {
1010
lang?: string,
1111
head?: string[],
12-
scripts?: (string | { id?: string, type?: string, src?: string, innerText?: string, async?: boolean, preload?: boolean })[],
12+
scripts?: (string | { id?: string, type?: string, src?: string, innerText?: string, nomodule?: boolean, async?: boolean, preload?: boolean })[],
1313
body: string,
1414
minify?: boolean
1515
}) {

polyfill.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export const nomoduleJS = (isDev: boolean) => {
2+
const indent = isDev ? ' ' : ''
3+
const eol = isDev ? '\n' : ''
4+
let title = 'Your browser is out of date.'
5+
let desc = 'Update your browser for more security, speed and the best experience on this site.'
6+
if (isDev) {
7+
desc = 'Aleph.js requires <a href="https://caniuse.com/es6-module" style="font-weight:500;color:#000;">ES module</a> support during development.'
8+
}
9+
return [
10+
``,
11+
`var el = document.querySelector("main");`,
12+
`var style = {`,
13+
`${indent}position: "fixed",`,
14+
`${indent}top: "50%",`,
15+
`${indent}left: "5%",`,
16+
`${indent}width: "90%",`,
17+
`${indent}height: "24px",`,
18+
`${indent}marginTop: "-12px",`,
19+
`${indent}lineHeight: "24px",`,
20+
`${indent}fontSize: "14px",`,
21+
`${indent}color: "#666",`,
22+
`${indent}textAlign: "center"`,
23+
'};',
24+
`el.innerHTML = '<p><strong style="font-size:24px;font-weight:700;color:#000;">${title}</strong><br>${desc}</p>';`,
25+
`for (var key in style) {`,
26+
`${indent}el.style[key] = style[key];`,
27+
`}`,
28+
].join(eol + indent.repeat(2)) + eol + indent
29+
}

project.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AlephAPIRequest, AlephAPIResponse } from './api.ts'
55
import { EventEmitter } from './events.ts'
66
import { createHtml } from './html.ts'
77
import log from './log.ts'
8+
import { nomoduleJS } from './polyfill.ts'
89
import { Routing } from './router.ts'
910
import { colors, ensureDir, path, ServerRequest, Sha1, walk } from './std.ts'
1011
import { compile } from './tsc/compile.ts'
@@ -193,8 +194,8 @@ export class Project {
193194
lang: url.locale,
194195
head: head,
195196
scripts: [
196-
// { type: 'application/json', id: 'ssr-data', innerText: JSON.stringify({ url }) },
197197
{ src: path.join(baseUrl, `/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' },
198+
{ innerText: nomoduleJS(this.isDev), nomodule: true },
198199
],
199200
body,
200201
minify: !this.isDev
@@ -209,6 +210,7 @@ export class Project {
209210
lang: defaultLocale,
210211
scripts: [
211212
{ src: path.join(baseUrl, `/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' },
213+
{ innerText: nomoduleJS(this.isDev), nomodule: true },
212214
],
213215
body: `<main></main>`,
214216
minify: !this.isDev
@@ -220,7 +222,7 @@ export class Project {
220222
const mod = this.#modules.get('/data.js')
221223
if (mod) {
222224
try {
223-
const { default: Data } = await import("file://" + mod.jsFile)
225+
const { default: Data } = await import('file://' + mod.jsFile)
224226
let data: any = Data
225227
if (util.isFunction(Data)) {
226228
data = await Data()
@@ -334,7 +336,7 @@ export class Project {
334336
log.fatal('parse config.json:', e.message)
335337
}
336338
} else {
337-
const { default: conf } = await import("file://" + p)
339+
const { default: conf } = await import('file://' + p)
338340
if (util.isPlainObject(conf)) {
339341
Object.assign(config, conf)
340342
log.debug(name, config)

0 commit comments

Comments
 (0)