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

Introduce app-root #5423

Merged
merged 18 commits into from
Oct 5, 2023
Merged
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
4 changes: 4 additions & 0 deletions bin/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ printf 'Building `fe-content-pages`...\n'
yarn workspace @zooniverse/fe-content-pages build
printf '\n'

printf 'Building `app-root`...\n'
yarn workspace @zooniverse/app-root build
printf '\n'

echo 'Done!'
4 changes: 4 additions & 0 deletions bin/bootstrap:es6.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ printf 'Building `fe-content-pages`...\n'
yarn workspace @zooniverse/fe-content-pages build
printf '\n'

printf 'Building `app-root`...\n'
yarn workspace @zooniverse/app-root build
printf '\n'

echo 'Done!'
1 change: 1 addition & 0 deletions docs/arch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@
- [ADR 51: User Group Stats Page](adr-51.md)
- [ADR 52: User Stats Page](adr-52.md)
- [ADR 53: Logged-In User Homepage](adr-53.md)
- [ADR 54: app-root](adr-54.md)
22 changes: 22 additions & 0 deletions docs/arch/adr-54.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# app-root

## Context
Upon the release of Next.js 13, we've decided to build app-root with the App Router - utilizing Next.js 13's ability to modularize the Zooniverse website. For instance, when a volunteer visits an /about page, the Javascript bundle their browser downloads should not contain /projects or any other unrelated route's code. This decision is also related to the addition and development of `/users` stats pages and `/groups` stats pages (ADRs 51 - 53).

## Decision
app-root includes
- The Zooniverse homepage components
- User auth (client-side)
- Layout with ZooHeader and ZooFooter
- App Router file structure for `/`, `/projects`, `/about`, `/users`, `/groups`, and more in the future.

## Consequences

FEM currently consists of two deployed apps: app-project and app-content-pages. Introducing app-root will put FEM on the path of a refactor app-project --> lib-project and app-content-pages --> lib-content-pages. Rather than each Zooniverse route existing as its own app, app-root will pull in code from the appropriate libraries (such as lib-user or lib-project). app-root will handle traffic for the domain root, https://zooniverse.org, so that it can serve the homepage.

User auth: In app-root we’re going to build a logged out homepage on the server, and then do client-side auth with the existing lib-panoptes-js to show user-specific content.

Common libraries used in FEM are Grommet, styled-components, mobx, storybook, mocha, and next-i18next. app-root is the first FEM app to use the Next.js App Router, and Next.js has become very opinionated about defaulting to React Server Components (RSC). Experimentation with the above mentioned libraries, App Router, and RSC is needed during development of app-root. In the scenario where one of these libraries is completely incompatible with App Router or RSC, further decisions will be made on whether to temporarily revert app-root to the Next.js Pages Router, or find a replacement for the incompatible library.

## Status
Accepted
35 changes: 35 additions & 0 deletions packages/app-root/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
36 changes: 36 additions & 0 deletions packages/app-root/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# app-root

A [Next.js](https://nextjs.org/) app for handling all routes, including `/projects`, `/user`, `/about`, `/groups`.

## Getting started

This package should be cloned as part of the [front-end-monorepo](https://github.com/zooniverse/front-end-monorepo).

## Running in development

Starts a development server on port 3000.

### Node
```sh
yarn dev
```
or

```sh
yarn build
yarn start
```

### Analyze bundle sizes

This app has `@next/bundle-analyzer` as a dev dependency. To use it, run `ANALYZE=true yarn build`.

## Technologies

- @zooniverse/panoptes-js - Panoptes API javascript client
- @zooniverse/react-components - Zooniverse common React components
- @zooniverse/grommet-theme - Zooniverse brand Grommet theme
- [Grommet](https://v2.grommet.io/components) - React UI component library
- [next.js](https://nextjs.org/) - Server-side rendering and routing framework
- [React.js](https://reactjs.org/) - Component, virtual DOM based javascript library
- [styled-components](https://www.styled-components.com/) - CSS in JS styling library.
7 changes: 7 additions & 0 deletions packages/app-root/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
9 changes: 9 additions & 0 deletions packages/app-root/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import withBundleAnalyzer from '@next/bundle-analyzer'

const bundleAnalyzer = withBundleAnalyzer({
enabled: process.env.ANALYZE === 'true',
})

const nextConfig = {}

export default bundleAnalyzer(nextConfig)
33 changes: 33 additions & 0 deletions packages/app-root/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@zooniverse/app-root",
"description": "",
"license": "Apache-2.0",
"author": "Zooniverse <contact@zooniverse.org> (https://www.zooniverse.org/)",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"type": "module",
goplayoutside3 marked this conversation as resolved.
Show resolved Hide resolved
"dependencies": {
"@zooniverse/async-states": "~0.0.1",
"@zooniverse/grommet-theme": "~3.1.1",
"@zooniverse/panoptes-js": "~0.4.1",
"@zooniverse/react-components": "~1.6.1",
"grommet": "~2.33.2",
"grommet-icons": "~4.11.0",
"next": "13.5.3",
"react": "~18.2.0",
"react-dom": "~18.2.0",
"styled-components": "~5.3.10"
},
"engines": {
"node": ">=18.13"
},
"devDependencies": {
"@next/bundle-analyzer": "~13.5.4"
}
}
7 changes: 7 additions & 0 deletions packages/app-root/src/app/about/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function AboutPage() {
return (
<div>
<p>This is lib-content-pages</p>
</div>
)
}
7 changes: 7 additions & 0 deletions packages/app-root/src/app/groups/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function UserGroupPage() {
return (
<div>
<p>This is lib-user</p>
</div>
)
}
17 changes: 17 additions & 0 deletions packages/app-root/src/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import RootLayout from '@/components/RootLayout'
import StyledComponentsRegistry from './style-registry'

export const metadata = {
title: 'Zooniverse',
description: 'People-powered Research'
}

export default function NextLayout({ children }) {
return (
<html lang='en'>
<StyledComponentsRegistry>
<RootLayout>{children}</RootLayout>
</StyledComponentsRegistry>
</html>
)
}
7 changes: 7 additions & 0 deletions packages/app-root/src/app/loading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function RootLoading() {
return (
<p>Loading...</p>
)
}

export default RootLoading
9 changes: 9 additions & 0 deletions packages/app-root/src/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function HomePage() {
return (
<main>
<div>
<p>This is the zooniverse.org home page</p>
</div>
</main>
)
}
7 changes: 7 additions & 0 deletions packages/app-root/src/app/projects/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function ProjectPage() {
return (
<div>
<p>This is lib-project</p>
</div>
)
}
29 changes: 29 additions & 0 deletions packages/app-root/src/app/style-registry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use client'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, so the server sends an unstyled page to the browser, then styles are built in the client?

Copy link
Contributor

@eatyourgreens eatyourgreens Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe not. The downloaded HTML includes an inline stylesheet. Vendor prefixes can definitely be removed here.

If we could move all this into an external CSS file, that would cost us less to build and serve, but I don't think that's possible with styled components and Next.js. At the moment, the server has to rebuild the CSS whenever the page is rebuilt (#3380), so whenever content changes. However, the CSS only changes once a week, when we deploy.
View Source for the new app, showing an inline <style> stylesheet in the page head.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 this stylesheet fixes the page styling issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breakpoints should be 48rem, not 768px, but that's probably a bug in the Grommet theme.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is odd. I built the app a second time. After the second build, I don't see the server-side stylesheet in the HTML, so it's unstyled again when it first loads.

Screen.Recording.2023-10-02.at.12.59.12.mov

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems ok after running the bootstrap script and building for a third time. 🤷‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know, thanks for double checking!


import { useServerInsertedHTML } from 'next/navigation'
import { ServerStyleSheet, StyleSheetManager } from 'styled-components'
import { useState } from 'react'

// https://nextjs.org/docs/app/building-your-application/styling/css-in-js#styled-components

export default function StyledComponentsRegistry({
children
}) {
// Only create stylesheet once with lazy initial state
// x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet())

useServerInsertedHTML(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hook name is confusing in a client-side component.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this is a client component, you noted earlier the downloaded HTML includes an inline stylesheet. Client components still run once on the server, and then again in the browser. This code is copied from the styled-components example: https://nextjs.org/docs/app/building-your-application/styling/css-in-js#styled-components

Do you recommend changing the function name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hook's name is set by Next.js, so that's fine. I was just confused as to how this component works.

const styles = styledComponentsStyleSheet.getStyleElement()
styledComponentsStyleSheet.instance.clearTag()
return <>{styles}</>
})

if (typeof window !== 'undefined') return <>{children}</>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks if the code is running in a browser, but the file specifies use client, so that's confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also a bit confused. I used the example from https://nextjs.org/docs/app/building-your-application/styling/css-in-js#styled-components, but don't totally understand why this line is included.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know I'm not the only one that's confused by how this works. 😄


return (
<StyleSheetManager disableVendorPrefixes sheet={styledComponentsStyleSheet.instance}>
{children}
</StyleSheetManager>
)
}
7 changes: 7 additions & 0 deletions packages/app-root/src/app/users/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function UserPage() {
return (
<div>
<p>This is lib-user</p>
</div>
)
}
37 changes: 37 additions & 0 deletions packages/app-root/src/components/RootLayout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use client'
/**
* Note that all child components are now client components.
* If we want children of RootLayout to be server components
* a ZooHeaderContainer and ZooFooterContainer could be created instead.
*/

import { createGlobalStyle } from 'styled-components'
import { Grommet } from 'grommet'
import zooTheme from '@zooniverse/grommet-theme'
import ZooHeader from '@zooniverse/react-components/ZooHeader'
import ZooFooter from '@zooniverse/react-components/ZooFooter'

const GlobalStyle = createGlobalStyle`
body {
margin: 0;
}
`

export default function RootLayout({ children }) {
return (
<body>
<GlobalStyle />
<Grommet
background={{
dark: 'dark-1',
light: 'light-1'
}}
theme={zooTheme}
>
<ZooHeader />
{children}
<ZooFooter />
</Grommet>
</body>
)
}
Loading