Skip to content

Commit

Permalink
feat: next.js example on Cloud Run
Browse files Browse the repository at this point in the history
  • Loading branch information
jthegedus committed May 24, 2020
1 parent c92d282 commit bb879dc
Show file tree
Hide file tree
Showing 28 changed files with 4,267 additions and 2,241 deletions.
12 changes: 12 additions & 0 deletions cloudrun-nextjs/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# blacklist everything
/*

# whitelist
#
!/Dockerfile
!/package.json
!/package-lock.json
!/next.config.js
!/components/
!/helpers/
!/pages/
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Created by https://www.gitignore.io/api/node,firebase
# Edit at https://www.gitignore.io/?templates=node,firebase

Expand Down
3 changes: 3 additions & 0 deletions cloudrun-nextjs/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
firebase 8.4.0
nodejs 12.16.3
gcloud 293.0.0
14 changes: 14 additions & 0 deletions cloudrun-nextjs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# COPY Files
FROM node:12-alpine AS build-env
WORKDIR /app
COPY . .
RUN npm install && npm run build

# Copy app and deps
FROM node:12-alpine
WORKDIR /app
COPY --from=build-env /app .
RUN rm -rf node_modules && npm install --only=production

# Run Next.js
CMD ["npm", "run", "start"]
53 changes: 53 additions & 0 deletions cloudrun-nextjs/components/Footer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function Footer() {
return (
<>
<footer>
Powered by
<a
href="https://firebase.google.com/products/hosting"
target="_blank"
rel="noopener noreferrer"
>
<img src="/firebase-hosting.svg" alt="Firebase Hosting Logo" />
</a>
&
<a
href="https://zeit.co?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<img src="/nextjs.svg" alt="Next.js Logo" className="nextjs" />
</a>
</footer>

<style jsx>{`
footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}
footer img {
max-height: 72px;
margin-left: 0.5rem;
}
footer a {
display: flex;
justify-content: center;
align-items: center;
}
footer img.nextjs {
margin-left: 1rem;
max-height: 48px;
}
`}</style>
</>
);
}

export default Footer;
18 changes: 18 additions & 0 deletions cloudrun-nextjs/components/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import Link from 'next/link';

export default function Header() {
return (
<header>
<Link href="/">
<a>Home</a>
</Link>{' '}
<Link href="/blog">
<a>Blog</a>
</Link>{' '}
<Link href="/about">
<a>About</a>
</Link>
</header>
);
}
19 changes: 19 additions & 0 deletions cloudrun-nextjs/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"firestore": {
"rules": "firestore.rules"
},
"hosting": {
"site": "TODO_YOUR_WEB_APP_DEPLOY_TARGET_HERE",
"public": "public",
"cleanUrls": true,
"rewrites": [
{
"source": "**",
"run": {
"serviceId": "nextjs-app",
"region": "us-central1"
}
}
]
}
}
File renamed without changes.
13 changes: 13 additions & 0 deletions cloudrun-nextjs/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function generatePosts(data) {
return data
? data.documents.map((post) => {
return {
pid: post.name.split('/').pop(),
title: post.fields.title.stringValue,
blurb: post.fields.blurb.stringValue,
};
})
: [];
}

export { generatePosts };
8 changes: 8 additions & 0 deletions cloudrun-nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
env: {
FIREBASE_PROJECT_ID: 'TODO_YOUR_PROJECT_ID_HERE',
},
experimental: {
sprFlushToDisk: false,
},
};
Loading

0 comments on commit bb879dc

Please sign in to comment.