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

Improve page performance with server-side rendering #50

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/out/

# production
/build
/dist

# misc
.DS_Store
Expand Down
Binary file modified bun.lockb
Binary file not shown.
22 changes: 0 additions & 22 deletions index.html

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "vite",
"build": "vite build",
"start": "vite preview",
"start": "vite build && vite preview",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives",
"test": "vitest"
},
Expand Down Expand Up @@ -38,6 +38,8 @@
"jsdom": "^22.1.0",
"postcss": "^8.4.38",
"tailwindcss": "^3.4.3",
"vike": "^0.4.179",
"vike-react": "^0.4.17",
"vite": "^5.2.12",
"vite-svg-loader": "^5.1.0",
"vitest": "^1.6.0"
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
1 change: 0 additions & 1 deletion src/assets/cloud.svg

This file was deleted.

34 changes: 16 additions & 18 deletions src/app/Flash.jsx → src/components/Flash.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useCallback } from 'react'
'use client';

import { useCallback, useEffect } from 'react'

import { Step, Error, useFastboot } from '@/utils/fastboot'

import bolt from '@/assets/bolt.svg'
import cable from '@/assets/cable.svg'
import cloud from '@/assets/cloud.svg'
import cloudDownload from '@/assets/cloud_download.svg'
import cloudError from '@/assets/cloud_error.svg'
import deviceExclamation from '@/assets/device_exclamation_c3.svg'
Expand All @@ -16,11 +17,6 @@ import systemUpdate from '@/assets/system_update_c3.svg'


const steps = {
[Step.INITIALIZING]: {
status: 'Initializing...',
bgColor: 'bg-gray-400 dark:bg-gray-700',
icon: cloud,
},
[Step.READY]: {
status: 'Ready',
description: 'Tap the button above to begin',
Expand Down Expand Up @@ -189,10 +185,6 @@ export default function Flash() {
serial,
} = useFastboot()

const handleContinue = useCallback(() => {
onContinue?.()
}, [onContinue])

const handleRetry = useCallback(() => {
onRetry?.()
}, [onRetry])
Expand All @@ -214,18 +206,24 @@ export default function Flash() {
}

// warn the user if they try to leave the page while flashing
if (Step.DOWNLOADING <= step && step <= Step.ERASING) {
window.addEventListener("beforeunload", beforeUnloadListener, { capture: true })
} else {
window.removeEventListener("beforeunload", beforeUnloadListener, { capture: true })
}
useEffect(() => {
if (Step.DOWNLOADING <= step && step <= Step.ERASING) {
window.addEventListener("beforeunload", beforeUnloadListener, { capture: true })
} else {
window.removeEventListener("beforeunload", beforeUnloadListener, { capture: true })
}

return () => {
window.removeEventListener("beforeunload", beforeUnloadListener, { capture: true })
}
}, [step])

return (
<div id="flash" className="relative flex flex-col gap-8 justify-center items-center h-full">
<div
className={`p-8 rounded-full ${bgColor}`}
style={{ cursor: onContinue ? 'pointer' : 'default' }}
onClick={handleContinue}
style={{ cursor: 'pointer' }}
onClick={onContinue}
>
<img
src={icon}
Expand Down
12 changes: 12 additions & 0 deletions src/layouts/Head.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import iconUrl from '../../public/icon.svg'

export default function HeadDefault() {
return (
<>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Update your comma device to the latest software" />
<link rel="icon" type="image/svg+xml" href={iconUrl} />
<script defer data-domain="flash.comma.ai" src="https://plausible.io/js/script.outbound-links.js"></script>
</>
);
}
9 changes: 9 additions & 0 deletions src/layouts/Layout.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import '../index.css'

import React from 'react'

export default function Layout({ children }) {
return (
<React.StrictMode>{children}</React.StrictMode>
)
}
14 changes: 0 additions & 14 deletions src/main.jsx

This file was deleted.

11 changes: 11 additions & 0 deletions src/pages/+config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import vikeReact from 'vike-react/config'
import Head from '../layouts/Head'
import Layout from '../layouts/Layout'

export default {
Head,
Layout,
title: "flash.comma.ai",
extends: vikeReact,
}

20 changes: 8 additions & 12 deletions src/app/index.jsx → src/pages/index/+Page.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Suspense, lazy } from 'react'
import Flash from '../../components/Flash'

import comma from '../assets/comma.svg'
import fastbootPorts from '../assets/fastboot-ports.svg'
import zadigCreateNewDevice from '../assets/zadig_create_new_device.png'
import zadigForm from '../assets/zadig_form.png'

const Flash = lazy(() => import('./Flash'))
import comma from '../../assets/comma.svg'
import fastbootPorts from '../../assets/fastboot-ports.svg'
import zadigCreateNewDevice from '../../assets/zadig_create_new_device.png'
import zadigForm from '../../assets/zadig_form.png'

export default function App() {
const version = import.meta.env.VITE_PUBLIC_GIT_SHA || 'dev'
console.info(`flash.comma.ai version: ${version}`);
const version = import.meta?.env?.VITE_PUBLIC_GIT_SHA ?? 'dev'
console.info(`flash.comma.ai version: ${version}`)
return (
<div className="flex flex-col lg:flex-row flex-wrap">
<main className="p-12 md:p-16 lg:p-20 xl:p-24 w-screen max-w-none lg:max-w-prose lg:w-auto h-auto lg:h-screen lg:overflow-y-auto prose dark:prose-invert prose-green bg-white dark:bg-gray-900">
Expand Down Expand Up @@ -149,9 +147,7 @@ export default function App() {
</main>

<div className="lg:flex-1 h-[700px] lg:h-screen bg-gray-100 dark:bg-gray-800">
<Suspense fallback={<p className="text-black dark:text-white">Loading...</p>}>
<Flash />
</Suspense>
<Flash />
</div>

<div className="w-screen max-w-none p-12 md:p-16 prose dark:prose-invert bg-white dark:bg-gray-900 lg:hidden">
Expand Down
5 changes: 2 additions & 3 deletions src/app/App.test.jsx → src/pages/index/Page.test.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Suspense } from 'react'
import { expect, test } from 'vitest'
import { render, screen } from '@testing-library/react'

import App from '.'
import Page from './+Page'

test('renders without crashing', () => {
render(<Suspense fallback="loading"><App /></Suspense>)
render(<Suspense fallback="loading"><Page /></Suspense>)
expect(screen.getByText('flash.comma.ai')).toBeInTheDocument()
})
16 changes: 16 additions & 0 deletions src/test/mockWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class MockWorker {
constructor(stringUrl) {
this.url = stringUrl;
this.onmessage = () => {};
}

postMessage(msg) {
this.onmessage({ data: msg });
}

addEventListener() {}
removeEventListener() {}
terminate() {}
}

export default MockWorker;
3 changes: 3 additions & 0 deletions src/test/setup.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import '@testing-library/jest-dom'
import MockWorker from './mockWorker';

global.Worker = MockWorker
Loading