Skip to content

Commit

Permalink
Fix prerendering
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 committed May 24, 2023
1 parent d44c017 commit 545f3b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
20 changes: 19 additions & 1 deletion packages/router/src/active-route-loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,31 @@ interface Props {
children?: React.ReactNode
}

let isPrerendered = false

if (typeof window !== 'undefined') {
const redwoodAppElement = document.getElementById('redwood-app')

if (redwoodAppElement && redwoodAppElement.children.length > 0) {
isPrerendered = true
}
}

const firstLoad = true

export const ActiveRouteLoader = ({
spec,
params,
whileLoadingPage,
}: Props) => {
const announcementRef = useRef<HTMLDivElement>(null)
const LazyRouteComponent = spec.LazyComponent

const usePrerenderLoader =
globalThis.__REDWOOD__PRERENDERING || (isPrerendered && firstLoad)

const LazyRouteComponent = usePrerenderLoader
? spec.prerenderLoader(spec.name).default
: spec.LazyComponent

useEffect(() => {
// Make this hook a no-op if we're rendering in an iframe.
Expand Down
7 changes: 2 additions & 5 deletions packages/router/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ export function flattenSearchParams(
export interface Spec {
name: string
loader: () => Promise<{ default: React.ComponentType<unknown> }>
prerenderLoader: (name?: string) => { default: React.ComponentType<unknown> }
LazyComponent:
| React.LazyExoticComponent<React.ComponentType<unknown>>
| React.ComponentType<unknown>
Expand Down Expand Up @@ -417,6 +418,7 @@ export function normalizePage(
return {
name: specOrPage.name,
loader: async () => ({ default: specOrPage }),
prerenderLoader: () => ({ default: specOrPage }),
LazyComponent: specOrPage,
}
}
Expand Down Expand Up @@ -560,11 +562,6 @@ export function analyzeRoutes(

// @NOTE: A <Private> is also a Set
if (isSetNode(node)) {
console.log(`👉 \n ~ file: util.ts:563 ~ node:`, node)
console.log(
`👉 \n ~ file: util.ts:563 ~ isPrivateNode:`,
isPrivateNode(node)
)
const {
children,
whileLoadingPage: whileLoadingPageFromCurrentSet,
Expand Down

0 comments on commit 545f3b0

Please sign in to comment.