Skip to content

Commit

Permalink
refact!: replace createApp with createRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
Daydreamer-riri committed Jul 21, 2023
1 parent 8ab816a commit 679f26a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Static-site generation for React on Vite.
import { ViteReactSSG } from 'vite-react-ssg'
import routes from './App.tsx'

export const createApp = ViteReactSSG(
export const createRoot = ViteReactSSG(
// react-router-dom data routes
{ routes },
// function to have custom setups
Expand Down
7 changes: 1 addition & 6 deletions examples/multiple-pages/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@ import { ViteReactSSG } from 'vite-react-ssg'
import { routes } from './App.js'
import './index.css'

export const createApp = ViteReactSSG({ routes })

// export const createApp = ViteReactSSG(
// <App />,
// routes,
// )
export const createRoot = ViteReactSSG({ routes })
6 changes: 3 additions & 3 deletions src/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function ViteReactSSG(

const isClient = typeof window !== 'undefined'

async function createApp(client = false, routePath?: string) {
async function createRoot(client = false, routePath?: string) {
const browserRouter = client ? createBrowserRouter(routerOptions.routes) : undefined

const appRenderCallbacks: Function[] = []
Expand Down Expand Up @@ -69,7 +69,7 @@ export function ViteReactSSG(
const container = typeof rootContainer === 'string'
? document.querySelector(rootContainer)!
: rootContainer
const { router } = await createApp(true)
const { router } = await createRoot(true)
hydrateRoot(container, (
<HelmetProvider>
<SiteMetadataDefaults />
Expand All @@ -79,7 +79,7 @@ export function ViteReactSSG(
})()
}

return createApp
return createRoot
}

export { default as Head } from './components/Head'
8 changes: 4 additions & 4 deletions src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { renderPreloadLinks } from './preload-links'

export type Manifest = Record<string, string[]>

export type CreateAppFactory = (client: boolean, routePath?: string) => Promise<ViteReactSSGContext<true> | ViteReactSSGContext<false>>
export type CreateRootFactory = (client: boolean, routePath?: string) => Promise<ViteReactSSGContext<true> | ViteReactSSGContext<false>>

function DefaultIncludedRoutes(paths: string[], _routes: Readonly<RouteRecord[]>) {
// ignore dynamic routes
Expand Down Expand Up @@ -104,11 +104,11 @@ export async function build(ssgOptions: Partial<ViteReactSSGOptions> = {}, viteC

const _require = createRequire(import.meta.url)

const { createApp, includedRoutes: serverEntryIncludedRoutes }: { createApp: CreateAppFactory; includedRoutes: ViteReactSSGOptions['includedRoutes'] } = format === 'esm'
const { createRoot, includedRoutes: serverEntryIncludedRoutes }: { createRoot: CreateRootFactory; includedRoutes: ViteReactSSGOptions['includedRoutes'] } = format === 'esm'
? await import(serverEntry)
: _require(serverEntry)
const includedRoutes = serverEntryIncludedRoutes || configIncludedRoutes
const { routes } = await createApp(false)
const { routes } = await createRoot(false)

let routesPaths = includeAllRoutes
? routesToPaths(routes)
Expand All @@ -132,7 +132,7 @@ export async function build(ssgOptions: Partial<ViteReactSSGOptions> = {}, viteC
console.log('🚀 ~ file: build.ts:132 ~ build ~ routesPaths:', routesPaths)
queue.add(async () => {
try {
const appCtx = await createApp(false, route) as ViteReactSSGContext<true>
const appCtx = await createRoot(false, route) as ViteReactSSGContext<true>
const { app: innerApp, routes, initialState, triggerOnSSRAppRendered, transformState = serializeState } = appCtx

Check warning on line 136 in src/node/build.ts

View workflow job for this annotation

GitHub Actions / lint

'innerApp' is assigned a value but never used. Allowed unused vars must match /^_/u

Check warning on line 136 in src/node/build.ts

View workflow job for this annotation

GitHub Actions / lint

'initialState' is assigned a value but never used. Allowed unused vars must match /^_/u

Check warning on line 136 in src/node/build.ts

View workflow job for this annotation

GitHub Actions / lint

'transformState' is assigned a value but never used. Allowed unused vars must match /^_/u

const transformedIndexHTML = (await onBeforePageRender?.(route, indexHTML, appCtx))

Check warning on line 138 in src/node/build.ts

View workflow job for this annotation

GitHub Actions / lint

'transformedIndexHTML' is assigned a value but never used. Allowed unused vars must match /^_/u
Expand Down

0 comments on commit 679f26a

Please sign in to comment.