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 8 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!'
35 changes: 35 additions & 0 deletions packages/app-root/.gitignore
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is there a .gitignore file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are .gitignore files in app-project, app-content-pages, and lib-classifier. Should there not be one here?

Copy link
Contributor

Choose a reason for hiding this comment

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

I’m not sure. This one seems to be mostly duplicating the one at the root of the monorepo.

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
12 changes: 12 additions & 0 deletions packages/app-root/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# app-root

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

```sh
yarn build
yarn start
```
7 changes: 7 additions & 0 deletions packages/app-root/jsconfig.json
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this file do?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, is it a Visual Studio thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct! I'll delete this file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uh oh, this file is actually responsible for resolving paths. When I delete it there's an error:

Module not found: Can't resolve '@/components/RootLayout'
> 1 | import RootLayout from '@/components/RootLayout'

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
4 changes: 4 additions & 0 deletions packages/app-root/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {}

module.exports = nextConfig
goplayoutside3 marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions packages/app-root/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"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"
},
"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.2",
goplayoutside3 marked this conversation as resolved.
Show resolved Hide resolved
"react": "18.2.0",
"react-dom": "18.2.0",
goplayoutside3 marked this conversation as resolved.
Show resolved Hide resolved
"styled-components": "^5.3.10"
goplayoutside3 marked this conversation as resolved.
Show resolved Hide resolved
},
"engines": {
"node": ">=18.13"
}
}
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>
)
}
19 changes: 19 additions & 0 deletions packages/app-root/src/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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'>
<body style={{ margin: 0 }}>
goplayoutside3 marked this conversation as resolved.
Show resolved Hide resolved
<StyledComponentsRegistry>
<RootLayout>{children}</RootLayout>
</StyledComponentsRegistry>
</body>
goplayoutside3 marked this conversation as resolved.
Show resolved Hide resolved
</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
7 changes: 7 additions & 0 deletions packages/app-root/src/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function HomePage() {
return (
<div>
<p>This is the zooniverse.org home page</p>
</div>
)
}
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>
)
}
27 changes: 27 additions & 0 deletions packages/app-root/src/app/style-registry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'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'

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 sheet={styledComponentsStyleSheet.instance}>
goplayoutside3 marked this conversation as resolved.
Show resolved Hide resolved
{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>
)
}
24 changes: 24 additions & 0 deletions packages/app-root/src/components/RootLayout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use client'

import { Grommet } from 'grommet'
import zooTheme from '@zooniverse/grommet-theme'
import { base as baseTheme } from 'grommet'
import { ZooHeader, ZooFooter } from '@zooniverse/react-components'

const theme = { ...baseTheme, ...zooTheme }

export default function RootLayout({ children }) {
return (
<Grommet
background={{
dark: 'dark-1',
light: 'light-1'
}}
theme={theme}
>
<ZooHeader />
<main>{children}</main>
goplayoutside3 marked this conversation as resolved.
Show resolved Hide resolved
<ZooFooter />
</Grommet>
)
}
Loading