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

[RFC] Use SSG for frontend #824

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
14 changes: 13 additions & 1 deletion src/endpoints/nextjs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import next from 'next';
import { STATIC_SITE_PRODUCTION } from '../config.js';
import { PRODUCTION, STATIC_SITE_PRODUCTION } from '../config.js';
import {verbose, error} from '../utils/index.js';

const PORT = Number(process.env.PORT || 5000);
Expand All @@ -18,6 +18,18 @@ export async function handleRequest(req, res) {
if (!req.path.startsWith('/_next/')) {
verbose(`next.js handling request for "${req.path}"`)
}

/**
* An example of how you can share data between our express server
* and the nextJs server, to be accessed via `getServerSideProps`
* or similar to facilitate SSR.
* See <c98760c350a91a0c5641a6e676fc972c9f1932bb> as well
*/
req.expressData = {
STATIC_SITE_PRODUCTION,
PRODUCTION
}

await prepared;
await _handleRequest(req, res);
return res.end();
Expand Down
64 changes: 52 additions & 12 deletions static-site/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
import { Html, Head, Main, NextScript } from "next/document";

export default function Document() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
/**
* Page copied from https://github.com/vercel/next.js/blob/canary/examples/with-styled-components/pages/_document.tsx
* with modifications. See also
* <https://nextjs.org/docs/pages/building-your-application/routing/custom-document>
*/

import type { DocumentContext, DocumentInitialProps } from "next/document";
import Document from "next/document";
import { ServerStyleSheet } from "styled-components";
import { Html, Head, Main, NextScript} from "next/document";

export default class MyDocument extends Document {
static async getInitialProps(
ctx: DocumentContext,
): Promise<DocumentInitialProps> {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;

try {
// Run the React rendering logic synchronously
ctx.renderPage = () =>
originalRenderPage({
// Useful for wrapping the whole react tree
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
});

// Run the parent `getInitialProps`, it now includes the custom `renderPage`
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: [initialProps.styles, sheet.getStyleElement()],
};
} finally {
sheet.seal();
}
}

/** The following was our previous _document rendering, which I presume is identical from that within the parent `Document` class,
* as we can leave it out and the result seems the same. TO CHECK.
*/
render() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
19 changes: 19 additions & 0 deletions static-site/pages/example-ssr-on-request.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

export const getServerSideProps = (async ({req}) => {
console.log("getServerSideProps running");
/* could run API queries here if needed, and they'll be made
server-side upon request but will not be made by the client */
return { props: { expressData: req.expressData} }
});


const Index = ({expressData}) => {

return <div style={{fontSize: '20px', color: 'orange'}}>
{`The express server is running in ${expressData.PRODUCTION ? 'production' : 'development'} mode`}
<br/>
{`The next.js server is running in ${expressData.STATIC_SITE_PRODUCTION ? 'production' : 'development'} mode`}
</div>
}
export default Index;

3 changes: 1 addition & 2 deletions static-site/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import dynamic from 'next/dynamic'
const Index = dynamic(() => import("../src/pages/index"), {ssr: false})
import Index from "../src/pages/index";
export default Index;
7 changes: 6 additions & 1 deletion static-site/pages/pathogens.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
*
* Example of dynamic import but using ssr (which is the default dynamic import!)
*/

import dynamic from 'next/dynamic'
const Index = dynamic(() => import("../src/sections/pathogens"), {ssr: false})
const Index = dynamic(() => import("../src/sections/pathogens"), {ssr: true})
export default Index;
75 changes: 72 additions & 3 deletions static-site/pages/team.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,72 @@
import dynamic from 'next/dynamic'
const Index = dynamic(() => import("../src/pages/team"), {ssr: false})
export default Index;

/**
*
* Example of a typical/simple (?) page for Next.js (pages), i.e. no dynamic imports.
*/

import React from "react";
import styled from "styled-components";
import GenericPage from "../src/layouts/generic-page";
import { BigSpacer, FlexCenter} from "../src/layouts/generalComponents";
import { CenteredFocusParagraph } from "../src/components/splash/styles";
import { ListOfPeople } from "../src/components/People/avatars";

const H1 = styled.div`
text-align: center;
font-size: 32px;
line-height: 32px;
font-weight: 300;
color: ${(props) => props.theme.darkGrey};
min-width: 240px;
margin-top: 0px;
margin-bottom: 20px;
`;

const TeamPage = () => {
console.log("<TeamPage>");
return (
<div>
<H1>Nextstrain core team</H1>

<FlexCenter>
<CenteredFocusParagraph>
{"Nextstrain was co-founded by:"}
</CenteredFocusParagraph>
</FlexCenter>

<ListOfPeople people="founders" teamPage />

<FlexCenter>
<CenteredFocusParagraph>
{"The core team currently working on Nextstrain are:"}
</CenteredFocusParagraph>
</FlexCenter>

<ListOfPeople people="core" teamPage />

<H1>Nextstrain Alumni</H1>
<FlexCenter>
<CenteredFocusParagraph>
{`Our previous core Nextstrain team members, some of whom are still working on projects involving Nextstrain and/or maintaining specific analyses. `}
{"Beyond the core team there have been many code contributions from the wider scientific and programming community; please see "}
<a href="https://github.com/nextstrain">our GitHub organization</a>
{" to see the history of (code) contributions."}
</CenteredFocusParagraph>
</FlexCenter>

<BigSpacer/>

<ListOfPeople people="alumni" teamPage />
</div>
);
};


const Team = props => (
<GenericPage location={props.location} footerProps={{showTeamAvatars: false}}>
<TeamPage/>
</GenericPage>
);

export default Team;

3 changes: 2 additions & 1 deletion static-site/src/components/Footer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const SplashImagesCredit = () => (

class Footer extends React.Component {
render() {
const showTeamAvatars = this.props.showTeamAvatars!==false; // same as a functional component with default of true
return (
<div>
<div className="row">
Expand All @@ -62,7 +63,7 @@ class Footer extends React.Component {
{"Hadfield "}<i>{"et al., "}</i>
<a href="https://doi.org/10.1093/bioinformatics/bty407" target="_blank" rel="noreferrer noopener">Nextstrain: real-time tracking of pathogen evolution</a>
<i>, Bioinformatics</i> (2018)
{(typeof window !== 'undefined' && window.location.pathname.replace(/\//g, "")!=="team") && (
{showTeamAvatars && (
<>
<div style={{margin: "10px 0px"}}/>
The core Nextstrain team is
Expand Down
4 changes: 2 additions & 2 deletions static-site/src/layouts/generic-page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import UserDataWrapper from "../layouts/userDataWrapper";
import { BigSpacer, HugeSpacer, Line } from "../layouts/generalComponents";
import * as splashStyles from "../components/splash/styles";

const GenericPage = ({location, children, banner}) => (
const GenericPage = ({location, children, banner, footerProps}) => (
<MainLayout>
<div className="index-container">
<Helmet title={siteTitle} />
Expand All @@ -19,7 +19,7 @@ const GenericPage = ({location, children, banner}) => (
<HugeSpacer /><HugeSpacer />
{children}
<Line style={{ margin: "30px 0px 10px 0px" }} />
<Footer />
<Footer {...footerProps} />
<BigSpacer />
</splashStyles.Container>
</UserDataWrapper>
Expand Down
66 changes: 0 additions & 66 deletions static-site/src/pages/team.jsx

This file was deleted.