Skip to content

Commit

Permalink
feat: optionally trigger frontend reload on bundle rebuild
Browse files Browse the repository at this point in the history
Set VITE_BUNDLE_SSE to cause the frontend to subscribe to server
sent events from the VITE_BUNDLE_HOST at /sse.
  • Loading branch information
jamesdabbs committed Dec 29, 2023
1 parent 6f5e378 commit cc6ac44
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 97 deletions.
1 change: 1 addition & 0 deletions packages/compile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/express": "^4.17.17",
"@types/glob": "^8.1.0",
"@types/yaml-front-matter": "^4.1.0",
"better-sse": "^0.10.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"tsheredoc": "^1.0.1",
Expand Down
9 changes: 9 additions & 0 deletions packages/compile/src/watch/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from 'chalk'
import cors from 'cors'
import express, { Express, NextFunction, Request, Response } from 'express'
import sse from 'better-sse'

import { bundle } from '@pi-base/core'

Expand All @@ -19,8 +20,11 @@ export function boot({ log, port }: { log: Logger; port: number }): {
} {
let state: State = { bundle: undefined, errors: undefined }

const reloadChannel = sse.createChannel()

function setState(updates: Partial<State>) {
state = { ...state, ...updates }
reloadChannel.broadcast({ version: state.bundle?.version }, 'bundle.reload')
}

const app = express()
Expand Down Expand Up @@ -55,6 +59,11 @@ export function boot({ log, port }: { log: Logger; port: number }): {
res.json(state.errors)
})

app.get('/sse', async (req, res) => {
const session = await sse.createSession(req, res)
reloadChannel.register(session)
})

app.listen(port, () => {
log(`Server running at http://localhost:${cyan(port)}.`)
})
Expand Down
6 changes: 1 addition & 5 deletions packages/viewer/src/components/Dev/Explore.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<script lang="ts">
import context from '@/context'
import Log from './Log.svelte'
import { reset } from '@/util'
const { spaces, properties, theorems, traits } = context()
function reset() {
localStorage.clear()
window.location.reload()
}
</script>

<h4>Entities</h4>
Expand Down
26 changes: 15 additions & 11 deletions packages/viewer/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { bundle } from '@pi-base/core'
import { dev } from '$app/environment'
import * as envPublic from '$env/static/public'

export const mainBranch = 'main'
export const contributingUrl = `https://github.com/pi-base/data/wiki/Contributing`
let _defaultHost = bundle.defaultHost
try {
_defaultHost = dev ? envPublic.PUBLIC_DATA_URL : bundle.defaultHost
} catch {}
export const defaultHost = _defaultHost
const {
VITE_BUNDLE_HOST = bundle.defaultHost,
VITE_BUNDLE_SSE = 'false',
VITE_BRANCH = 'unknown',
VITE_MAIN_BRANCH = 'main',
VITE_VERSION = 'unknown',
} = import.meta.env

export const mainBranch = VITE_MAIN_BRANCH
export const defaultHost = VITE_BUNDLE_HOST

export const build = {
branch: import.meta.env.VITE_BRANCH || 'unknown',
version: import.meta.env.VITE_VERSION || 'unknown',
branch: VITE_BRANCH,
version: VITE_VERSION,
}

export const bundleSse = VITE_BUNDLE_SSE === 'true'

export const contributingUrl = `https://github.com/pi-base/data/wiki/Contributing`
export const sentryIngest =
'https://[email protected]/5251960'
11 changes: 11 additions & 0 deletions packages/viewer/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<script lang="ts">
import 'bootstrap/dist/css/bootstrap.min.css'
import { browser } from '$app/environment'
import type { LayoutData } from './$types'
import { bundleSse, defaultHost } from '@/constants'
import { set } from '@/context'
import { reset } from '@/util'
import Nav from '@/components/Nav.svelte'
import Status from '@/components/Status.svelte'
Expand All @@ -19,6 +23,13 @@
// so that it doesn't get thrown away and rebuilt when the user happens to
// navigate to a page without any rendered math.
data.typeset.subscribe(() => {})
if (browser && bundleSse) {
new EventSource(`${defaultHost}/sse`).addEventListener(
'bundle.reload',
reset,
)
}
</script>

<Nav />
Expand Down
26 changes: 14 additions & 12 deletions packages/viewer/src/routes/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,35 @@ import * as errors from '@/errors'
import { sync } from '@/gateway'
import type { LayoutLoad } from './$types'

const bundleHost = import.meta.env.VITE_BUNDLE_HOST || defaultHost

export const load: LayoutLoad = async ({ fetch, url: { host } }) => {
const dev = host.match(/(dev(elopment)?[.-]|localhost)/) !== null
const errorEnv: errors.Environment = [
'topology.pi-base.org',
'topology.pages.dev',
].includes(host)
? 'production'
: host.includes('pages.dev')
? 'deploy-preview'
: 'dev'

const errorHandler = dev
? errors.log()
: errors.sentry({ environment: errorEnv })
: errors.sentry({ environment: errorEnv(host) })

const context = initialize({
showDev: dev,
errorHandler,
gateway: sync(fetch),
source: {
host: bundleHost,
host: defaultHost,
},
})

await context.loaded()

return context
}

function errorEnv(host: string): errors.Environment {
if (['topology.pi-base.org', 'topology.pages.dev'].includes(host)) {
return 'production'
}

if (host.includes('pages.dev')) {
return 'deploy-preview'
}

return 'dev'
}
1 change: 0 additions & 1 deletion packages/viewer/src/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { defaultHost, mainBranch } from '@/constants'
import type * as Gateway from '@/gateway'
import {
Collection,
Expand Down
5 changes: 5 additions & 0 deletions packages/viewer/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ export function subscribeUntil<S>(
export function capitalize(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1)
}

export function reset() {
localStorage.clear()
window.location.reload()
}
6 changes: 3 additions & 3 deletions packages/viewer/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export default defineConfig({
test: {
include: ['src/**/*.{test,spec}.{js,ts}'],
coverage: {
lines: 83.09,
lines: 82.91,
branches: 87.36,
statements: 83.09,
functions: 81.81,
statements: 82.91,
functions: 80.19,
skipFull: true,
thresholdAutoUpdate: true,
},
Expand Down
82 changes: 17 additions & 65 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cc6ac44

Please sign in to comment.