Skip to content

Commit

Permalink
Use Next.js Runtime Environment Variables (#26)
Browse files Browse the repository at this point in the history
* use runtime environment variables
* fixes https://codeberg.org/nesaku/BiblioReads/issues/85
  • Loading branch information
nesaku authored Nov 5, 2024
1 parent aa42d17 commit 7f58be7
Show file tree
Hide file tree
Showing 25 changed files with 188 additions and 112 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ next-env.d.ts
**/public/worker-*.js
**/public/worker-*.js.map
**/public/workbox-*.js
**/public/workbox-*.js.map
**/public/workbox-*.js.map

# Auto Generated Runtime ENV files
**/public/__ENV.js
**/public/fallback-*.js
9 changes: 4 additions & 5 deletions components/aboutpage/FAQ.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from "next-runtime-env";
import React from "react";

const FAQ = () => {
Expand Down Expand Up @@ -56,13 +57,11 @@ const FAQ = () => {
1. Visit{" "}
<a
className="underline text-green-600 dark:text-green-500/80"
href={
process.env.NEXT_PUBLIC_HOST_URL || "http://localhost:3000"
}
href={env("NEXT_PUBLIC_HOST_URL") || "http://localhost:3000"}
target="_blank"
rel="noreferrer"
>
{process.env.NEXT_PUBLIC_HOST_URL || "http://localhost:3000"}
{env("NEXT_PUBLIC_HOST_URL") || "http://localhost:3000"}
</a>{" "}
and either paste the Goodreads book URL into the input box or
search for a book using the input box.
Expand All @@ -74,7 +73,7 @@ const FAQ = () => {
</span>{" "}
of any book page URL with{" "}
<span className="text-green-600 dark:text-green-500/80">
{process.env.NEXT_PUBLIC_HOST_URL || "http://localhost:3000"}
{env("NEXT_PUBLIC_HOST_URL") || "http://localhost:3000"}
</span>
.
</p>
Expand Down
9 changes: 5 additions & 4 deletions components/contactpage/ContactForm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from "next-runtime-env";
import Link from "next/link";
import React from "react";

Expand All @@ -6,8 +7,8 @@ const ContactForm = () => {
<div className="px-4 py-8 mb-20 mx-auto max-w-screen-sm text-center backdrop-blur-lg rounded-2xl">
<form
action={
process.env.NEXT_PUBLIC_CONTACT_FORM_SUBMISSION
? process.env.NEXT_PUBLIC_CONTACT_FORM_SUBMISSION
env("NEXT_PUBLIC_CONTACT_FORM_SUBMISSION")
? env("NEXT_PUBLIC_CONTACT_FORM_SUBMISSION")
: "https://submit-form.com/4GkO3Bo7"
}
method="POST"
Expand All @@ -18,8 +19,8 @@ const ContactForm = () => {
type="hidden"
name="_redirect"
value={
process.env.NEXT_PUBLIC_HOST_URL
? `${process.env.NEXT_PUBLIC_HOST_URL}/success`
env("NEXT_PUBLIC_HOST_URL")
? `${env("NEXT_PUBLIC_HOST_URL")}/success`
: "https://biblioreads.eu.org/success"
}
/>
Expand Down
9 changes: 5 additions & 4 deletions components/contactpage/ContactHero.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { env } from "next-runtime-env";
import React from "react";

const ContactHero = () => {
return (
/* <div className="mx-auto max-w-screen-xl min-h-[70vh] px-4 pt-20 pb-4 lg:flex lg:mt-20"> */
<div
className={`mx-auto max-w-screen-xl px-4 pt-20 pb-4 lg:flex lg:mt-20 ${
process.env.NEXT_PUBLIC_ENABLE_CONTACT_FORM != "true" && "min-h-[70vh]"
env("NEXT_PUBLIC_ENABLE_CONTACT_FORM") != "true" && "min-h-[70vh]"
}`}
>
<div className="mx-auto max-w-3xl text-center text-gray-900 dark:text-white ">
Expand Down Expand Up @@ -36,14 +37,14 @@ const ContactHero = () => {
</span>
</a>
.
{process.env.NEXT_PUBLIC_ENABLE_CONTACT_FORM === "true"
{env("NEXT_PUBLIC_ENABLE_CONTACT_FORM") === "true"
? " Alternatively, you can use the contact form below."
: process.env.NEXT_PUBLIC_INSTANCE_OPERATOR_LINK && (
: env("NEXT_PUBLIC_INSTANCE_OPERATOR_LINK") && (
<div className="mt-16 text-xl h-[45vh]">
You can reach the owner of this instance{" "}
<a
className="group text-blue-600 dark:text-blue-500 transition-all duration-300 ease-in-out"
href={process.env.NEXT_PUBLIC_INSTANCE_OPERATOR_LINK}
href={env("NEXT_PUBLIC_INSTANCE_OPERATOR_LINK")}
target="_blank"
rel="noreferrer"
>
Expand Down
5 changes: 3 additions & 2 deletions components/global/Meta.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import Head from "next/head";
import { env } from "next-runtime-env";

const Meta = (props) => {
const title = `BiblioReads ${props.title ? `- ${props.title}` : ""} `;
Expand Down Expand Up @@ -46,10 +47,10 @@ const Meta = (props) => {
<meta property="og:description" content={descriptionOG} />
<meta property="og:site_name" content="BiblioReads" />
<meta property="og:type" content="website" />
{process.env.NEXT_PUBLIC_HOST_URL && (
{env("NEXT_PUBLIC_HOST_URL") && (
<meta
property="og:image"
content={`${process.env.NEXT_PUBLIC_HOST_URL}/social.png`}
content={`${env("NEXT_PUBLIC_HOST_URL")}/social.png`}
/>
)}
</Head>
Expand Down
5 changes: 5 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/** @type {import('next').NextConfig} */

const { configureRuntimeEnv } = require("next-runtime-env/build/configure");

configureRuntimeEnv();

const withPWA = require("next-pwa")({
dest: "public",
register: true,
Expand Down
121 changes: 72 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"idb": "^8.0.0",
"next": "12.3.1",
"next-pwa": "^5.6.0",
"next-runtime-env": "^1.7.4",
"next-themes": "^0.2.1",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
16 changes: 16 additions & 0 deletions pages/_document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Html, Head, Main, NextScript } from "next/document";

export default function Document() {
return (
<Html lang="en">
<Head>
{/* eslint-disable-next-line @next/next/no-sync-scripts */}
<script src="/__ENV.js" />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
Loading

0 comments on commit 7f58be7

Please sign in to comment.