Skip to content

Commit

Permalink
chore(rsc): Make Router-related imports safer for RSC (#11644)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored and Josh-Walker-GM committed Oct 8, 2024
1 parent 3863f0d commit 8cd91ce
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/internal/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import chalk from 'chalk'

import { getPaths, getRouteHookForPage } from '@redwoodjs/project-config'
import { getRouteRegexAndParams } from '@redwoodjs/router'
import { getRouteRegexAndParams } from '@redwoodjs/router/dist/util'
import { getProject } from '@redwoodjs/structure/dist/index.js'
import type { RWRoute } from '@redwoodjs/structure/dist/model/RWRoute'

Expand Down
1 change: 0 additions & 1 deletion packages/router/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-var */
/* eslint-disable no-var */
/// <reference types="react/experimental" />

declare global {
Expand Down
4 changes: 4 additions & 0 deletions packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"import": "./dist/util.js",
"require": "./dist/cjs/util.js"
},
"./react-util": {
"import": "./dist/react-util.js",
"require": "./dist/cjs/react-util.js"
},
"./rscCss": {
"require": "./dist/cjs/rsc/rscCss.js",
"import": "./dist/rsc/rscCss.js"
Expand Down
14 changes: 14 additions & 0 deletions packages/router/src/react-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { ReactNode } from 'react'
import { Children, isValidElement } from 'react'

export function flattenAll(children: ReactNode): ReactNode[] {
const childrenArray = Children.toArray(children)

return childrenArray.flatMap((child) => {
if (isValidElement(child) && child.props.children) {
return [child, ...flattenAll(child.props.children)]
}

return [child]
})
}
14 changes: 0 additions & 14 deletions packages/router/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
// These are utils that can be shared between both server- and client components

import type { ReactNode } from 'react'
import { Children, isValidElement } from 'react'

export function flattenAll(children: ReactNode): ReactNode[] {
const childrenArray = Children.toArray(children)

return childrenArray.flatMap((child) => {
if (isValidElement(child) && child.props.children) {
return [child, ...flattenAll(child.props.children)]
}

return [child]
})
}
/**
* Get param name, type, and match for a route.
*
Expand Down
3 changes: 2 additions & 1 deletion packages/storybook/src/mocks/MockRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import type React from 'react'

import { flattenAll } from '@redwoodjs/router/dist/react-util'
import { isValidRoute } from '@redwoodjs/router/dist/route-validators'
import type { RouterProps } from '@redwoodjs/router/dist/router'
import { flattenAll, replaceParams } from '@redwoodjs/router/dist/util'
import { replaceParams } from '@redwoodjs/router/dist/util'

export * from '@redwoodjs/router/dist/index'

Expand Down
6 changes: 2 additions & 4 deletions packages/testing/src/web/MockRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import type React from 'react'

import { flattenAll } from '@redwoodjs/router/dist/react-util'
// Bypass the `main` field in `package.json` because we alias `@redwoodjs/router`
// for jest and Storybook. Not doing so would cause an infinite loop.
// See: ./packages/testing/config/jest/web/jest-preset.js
// @ts-ignore
import { isValidRoute } from '@redwoodjs/router/dist/route-validators'
import type { RouterProps } from '@redwoodjs/router/dist/router'
import { flattenAll, replaceParams } from '@redwoodjs/router/dist/util'
// @ts-ignore
import { replaceParams } from '@redwoodjs/router/dist/util'
export * from '@redwoodjs/router/dist/index'

export const routes: { [routeName: string]: () => string } = {}
Expand Down

0 comments on commit 8cd91ce

Please sign in to comment.