Skip to content

Commit

Permalink
RSC: Use pages directory (#9660)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Dec 12, 2023
1 parent 09d4fe0 commit 809e3ff
Show file tree
Hide file tree
Showing 30 changed files with 184 additions and 27 deletions.
11 changes: 9 additions & 2 deletions __fixtures__/test-project-rsa/web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { FatalErrorBoundary } from '@redwoodjs/web'
import { FatalErrorBoundary, RedwoodProvider } from '@redwoodjs/web'
import { RedwoodApolloProvider } from '@redwoodjs/web/apollo'

import FatalErrorPage from './pages/FatalErrorPage/FatalErrorPage'
import Routes from './Routes'

import './index.css'

const App = () => (
<FatalErrorBoundary page={FatalErrorPage}>
<Routes />
<RedwoodProvider titleTemplate="%PageTitle | %AppTitle">
<RedwoodApolloProvider>
<Routes />
</RedwoodApolloProvider>
</RedwoodProvider>
</FatalErrorBoundary>
)

Expand Down
4 changes: 2 additions & 2 deletions __fixtures__/test-project-rsa/web/src/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export default defineEntries(
async (id) => {
switch (id) {
case 'AboutPage':
return import('./AboutPage')
return import('./pages/AboutPage/AboutPage')
case 'HomePage':
return import('./HomePage')
return import('./pages/HomePage/HomePage')
default:
return null
}
Expand Down
1 change: 0 additions & 1 deletion __fixtures__/test-project-rsa/web/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/png" href="/favicon.png" />
<link rel="stylesheet" href="index.css" />
<script type="module" src="entry.client.tsx"></script>
</head>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Assets } from '@redwoodjs/vite/assets'
import { ProdRwRscServerGlobal } from '@redwoodjs/vite/rwRscGlobal'

import { AboutCounter } from './AboutCounter'
import { AboutCounter } from '../../components/Counter/AboutCounter'

import './AboutPage.css'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { RscForm } from '@tobbe.dev/rsc-test'
import { Assets } from '@redwoodjs/vite/assets'
import { ProdRwRscServerGlobal } from '@redwoodjs/vite/rwRscGlobal'

import { Counter } from '../../components/Counter/Counter'
import { onSend } from './actions'
import { Counter } from './Counter'
// @ts-expect-error no types
import styles from './HomePage.module.css'

Expand Down
11 changes: 9 additions & 2 deletions __fixtures__/test-project-rsc-external-packages/web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { FatalErrorBoundary } from '@redwoodjs/web'
import { FatalErrorBoundary, RedwoodProvider } from '@redwoodjs/web'
import { RedwoodApolloProvider } from '@redwoodjs/web/apollo'

import FatalErrorPage from './pages/FatalErrorPage/FatalErrorPage'
import Routes from './Routes'

import './index.css'

const App = () => (
<FatalErrorBoundary page={FatalErrorPage}>
<Routes />
<RedwoodProvider titleTemplate="%PageTitle | %AppTitle">
<RedwoodApolloProvider>
<Routes />
</RedwoodApolloProvider>
</RedwoodProvider>
</FatalErrorBoundary>
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client'

import React from 'react'

// @ts-expect-error no types
import styles from './Counter.module.css'
import './Counter.css'

export const AboutCounter = () => {
const [count, setCount] = React.useState(0)

return (
<div style={{ border: '3px blue dashed', margin: '1em', padding: '1em' }}>
<p>Count: {count}</p>
<button onClick={() => setCount((c) => c + 1)}>Increment</button>
<h3 className={styles.header}>This is a client component.</h3>
<p>RSC on client: {globalThis.RWJS_EXP_RSC ? 'enabled' : 'disabled'}</p>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This should affect all h3 elements on the page, both server components and
* client components. This is just standard CSS stuff
*/
h3 {
color: orange;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.header {
font-style: italic;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use client'

import React from 'react'

import 'client-only'

// @ts-expect-error no types
import styles from './Counter.module.css'
import './Counter.css'

export const Counter = () => {
const [count, setCount] = React.useState(0)

return (
<div style={{ border: '3px blue dashed', margin: '1em', padding: '1em' }}>
<p>Count: {count}</p>
<button onClick={() => setCount((c) => c + 1)}>Increment</button>
<h3 className={styles.header}>This is a client component.</h3>
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export default defineEntries(
async (id) => {
switch (id) {
case 'AboutPage':
return import('./AboutPage')
return import('./pages/AboutPage/AboutPage')
case 'HomePage':
return import('./HomePage')
return import('./pages/HomePage/HomePage')
default:
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/png" href="/favicon.png" />
<link rel="stylesheet" href="index.css" />
<script type="module" src="entry.client.tsx"></script>
</head>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.about-page {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Assets } from '@redwoodjs/vite/assets'
import { ProdRwRscServerGlobal } from '@redwoodjs/vite/rwRscGlobal'

import { AboutCounter } from '../../components/Counter/AboutCounter'

import './AboutPage.css'

// TODO (RSC) Something like this will probably be needed
// const RwRscGlobal = import.meta.env.PROD ? ProdRwRscServerGlobal : DevRwRscServerGlobal;

globalThis.rwRscGlobal = new ProdRwRscServerGlobal()

const AboutPage = () => {
return (
<div className="about-page">
{/* TODO (RSC) <Assets /> should be part of the router later */}
<Assets />
<div style={{ border: '3px red dashed', margin: '1em', padding: '1em' }}>
<h1>About Redwood</h1>
<AboutCounter />
<p>RSC on server: {globalThis.RWJS_EXP_RSC ? 'enabled' : 'disabled'}</p>
</div>
</div>
)
}

export default AboutPage
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.home-page {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.title {
color: green;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { RscForm } from '@tobbe.dev/rsc-test'

import { Assets } from '@redwoodjs/vite/assets'
import { ProdRwRscServerGlobal } from '@redwoodjs/vite/rwRscGlobal'

import { Counter } from '../../components/Counter/Counter'
import { onSend } from './actions'
// @ts-expect-error no types
import styles from './HomePage.module.css'

import './HomePage.css'

// TODO (RSC) Something like this will probably be needed
// const RwRscGlobal = import.meta.env.PROD ? ProdRwRscServerGlobal : DevRwRscServerGlobal;

globalThis.rwRscGlobal = new ProdRwRscServerGlobal()

const HomePage = ({ name = 'Anonymous' }) => {
return (
<div className="home-page">
{/* TODO (RSC) <Assets /> should be part of the router later */}
<Assets />
<div style={{ border: '3px red dashed', margin: '1em', padding: '1em' }}>
<h1 className={styles.title}>Hello {name}!!</h1>
<RscForm onSend={onSend} />
<Counter />
</div>
</div>
)
}

export default HomePage
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
processPagesDir,
getPaths,
ensurePosixPath,
getConfig,
} from '@redwoodjs/project-config'

interface PluginOptions {
Expand Down Expand Up @@ -63,6 +64,14 @@ export default function (
)
}

if (getConfig().experimental?.rsc?.enabled) {
// TODO (RSC): Enable auto-loader for RSC
return {
name: 'babel-plugin-redwood-routes-auto-loader',
visitor: {},
}
}

return {
name: 'babel-plugin-redwood-routes-auto-loader',
visitor: {
Expand Down
36 changes: 26 additions & 10 deletions packages/cli/src/commands/experimental/setupRscHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ export const handler = async ({ force, verbose }) => {
),
'utf-8'
)
const homePagePath = path.join(rwPaths.web.src, 'HomePage.tsx')
const homePagePath = path.join(
rwPaths.web.pages,
'HomePage',
'HomePage.tsx'
)

writeFile(homePagePath, homePageTemplate, {
overwriteExisting: force,
Expand All @@ -117,7 +121,11 @@ export const handler = async ({ force, verbose }) => {
),
'utf-8'
)
const aboutPagePath = path.join(rwPaths.web.src, 'AboutPage.tsx')
const aboutPagePath = path.join(
rwPaths.web.pages,
'AboutPage',
'AboutPage.tsx'
)

writeFile(aboutPagePath, aboutPageTemplate, {
overwriteExisting: force,
Expand All @@ -131,7 +139,11 @@ export const handler = async ({ force, verbose }) => {
path.resolve(__dirname, 'templates', 'rsc', 'Counter.tsx.template'),
'utf-8'
)
const counterPath = path.join(rwPaths.web.src, 'Counter.tsx')
const counterPath = path.join(
rwPaths.web.components,
'Counter',
'Counter.tsx'
)

writeFile(counterPath, counterTemplate, {
overwriteExisting: force,
Expand All @@ -150,7 +162,11 @@ export const handler = async ({ force, verbose }) => {
),
'utf-8'
)
const counterPath = path.join(rwPaths.web.src, 'AboutCounter.tsx')
const counterPath = path.join(
rwPaths.web.components,
'Counter',
'AboutCounter.tsx'
)

writeFile(counterPath, counterTemplate, {
overwriteExisting: force,
Expand All @@ -163,23 +179,23 @@ export const handler = async ({ force, verbose }) => {
const files = [
{
template: 'Counter.css.template',
path: 'Counter.css',
path: ['components', 'Counter', 'Counter.css'],
},
{
template: 'Counter.module.css.template',
path: 'Counter.module.css',
path: ['components', 'Counter', 'Counter.module.css'],
},
{
template: 'HomePage.css.template',
path: 'HomePage.css',
path: ['pages', 'HomePage', 'HomePage.css'],
},
{
template: 'HomePage.module.css.template',
path: 'HomePage.module.css',
path: ['pages', 'HomePage', 'HomePage.module.css'],
},
{
template: 'AboutPage.css.template',
path: 'AboutPage.css',
path: ['pages', 'AboutPage', 'AboutPage.css'],
},
]

Expand All @@ -188,7 +204,7 @@ export const handler = async ({ force, verbose }) => {
path.resolve(__dirname, 'templates', 'rsc', file.template),
'utf-8'
)
const filePath = path.join(rwPaths.web.src, file.path)
const filePath = path.join(rwPaths.web.src, ...file.path)

writeFile(filePath, template, {
overwriteExisting: force,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Assets } from '@redwoodjs/vite/assets'
import { ProdRwRscServerGlobal } from '@redwoodjs/vite/rwRscGlobal'

import { AboutCounter } from './AboutCounter'
import { AboutCounter } from '../../components/Counter/AboutCounter'

import './AboutPage.css'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FatalErrorBoundary, RedwoodProvider } from '@redwoodjs/web'
import { RedwoodApolloProvider } from '@redwoodjs/web/apollo'

import FatalErrorPage from './pages/FatalErrorPage/FatalErrorPage'
import Routes from './Routes'
Expand All @@ -8,7 +9,9 @@ import './index.css'
const App = () => (
<FatalErrorBoundary page={FatalErrorPage}>
<RedwoodProvider titleTemplate="%PageTitle | %AppTitle">
<Routes />
<RedwoodApolloProvider>
<Routes />
</RedwoodApolloProvider>
</RedwoodProvider>
</FatalErrorBoundary>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Assets } from '@redwoodjs/vite/assets'
import { ProdRwRscServerGlobal } from '@redwoodjs/vite/rwRscGlobal'

import { Counter } from './Counter'
import { Counter } from '../../components/Counter/Counter'
// @ts-expect-error no types
import styles from './HomePage.module.css'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export default defineEntries(
async (id) => {
switch (id) {
case 'AboutPage':
return import('./AboutPage')
return import('./pages/AboutPage/AboutPage')
case 'HomePage':
return import('./HomePage')
return import('./pages/HomePage/HomePage')
default:
return null
}
Expand Down

0 comments on commit 809e3ff

Please sign in to comment.