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

Vercel Cron job to Trigger Fetching of Pages #35

Merged
merged 2 commits into from
Apr 20, 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
2 changes: 1 addition & 1 deletion components/MaybeLink.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styles from "./GoogleDocFormatter.module.scss";

import { isWrappedInSquareBrackets } from "./utils/format";
import { isWrappedInSquareBrackets } from "../utils/format";

const MaybeLink = ({ children, link }) => {
// check to see if the children text is wrapped in square brackets
Expand Down
4 changes: 2 additions & 2 deletions components/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useRouter } from "next/router";

import styles from "./Navigation.module.scss";
import cx from "classnames";
import { standardizePageId } from "./utils/format";
import { standardizePageId } from "../utils/format";

export const Navigation = ({ menuData, isMenuOpen, setIsMenuOpen }) => {
const { asPath } = useRouter();
Expand Down Expand Up @@ -48,7 +48,7 @@ export const Navigation = ({ menuData, isMenuOpen, setIsMenuOpen }) => {

const url =
pageTitle.toLowerCase() === "home" ||
pageTitle.toLowerCase() === "index"
pageTitle.toLowerCase() === "index"
? "/"
: `/${standardizePageId(pageTitle)}`;

Expand Down
34 changes: 5 additions & 29 deletions pages/[[...slug]].js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import GoogleDocFormatter from "../components/GoogleDocFormatter";

import styles from "../styles/Home.module.css";
import { Navigation } from "../components/Navigation";
import { standardizePageId } from "../components/utils/format";
import { standardizePageId } from "../utils/format";
import { getFileList } from "../utils/data";

export async function getStaticPaths() {
return {
Expand All @@ -19,33 +20,7 @@ export async function getStaticProps({ params }) {
const file = params.slug?.[0];
const fileName = file?.split("-").join(" ") ?? "home";

const client = new google.auth.JWT({
email: process.env.CLIENT_EMAIL,
scopes: [
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/drive",
],
key: process.env.PRIVATE_KEY,
});

await client.authorize();

// fetch the entire folder
const folderId = "11N3RY-5t73GvWfvXa-qFAvww8vIw1AOG";
const gsapi2 = google.drive({ version: "v3", auth: client });
const opt2 = {
q: `'${folderId}' in parents and trashed = false`,
};
let data2 = await gsapi2.files.list(opt2);
const fileList = data2.data.files
?.sort((a, b) => {
if (a.name && b.name) {
if (a?.name < b?.name) return -1;
if (a?.name > b?.name) return 1;
}
return 0;
})
.filter((item) => !item.mimeType.includes("folder"));
const [fileList, client] = await getFileList();

const homeDocumentId = fileList[0].id;

Expand All @@ -71,7 +46,7 @@ export async function getStaticProps({ params }) {
};
}

export default function Home({ data, menuData }) {
export default function Page({ data, menuData }) {
const [isMenuOpen, setIsMenuOpen] = useState(false);

if (typeof window !== "undefined") {
Expand All @@ -97,6 +72,7 @@ export default function Home({ data, menuData }) {
isMenuOpen={isMenuOpen}
setIsMenuOpen={setIsMenuOpen}
/>

<main id="mainContainer" className={styles.main}>
<GoogleDocFormatter rawData={data} />
</main>
Expand Down
1 change: 1 addition & 0 deletions pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default function Document() {
async
src="https://www.googletagmanager.com/gtag/js?id=G-9PKCZSQJ5F"
></script>
<link rel="icon" href="/favicon-32x32.png" />
<script
dangerouslySetInnerHTML={{
__html: `window.dataLayer = window.dataLayer || [];
Expand Down
22 changes: 22 additions & 0 deletions pages/api/cron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { standardizePageId } from "../../utils/format";
import { getFileList } from "../../utils/data";

export default async function handler(req, res) {
const [fileList, client] = await getFileList();
const urls = fileList.map((item) => {
return `https://running.goinvo.com/${standardizePageId(item.name)}`;
})
urls.push('https://running.goinvo.com');
urls.push('https://running.goinvo.com/index');

for (let i = 0; i < 4; i++) {
setTimeout(async () => {
for (const url of urls) {
console.log('visiting ' + url);
await fetch(url);
}
}, i * 3000);
}

res.status(200).end('Good!');
}
Binary file added public/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed public/favicon.ico
Binary file not shown.
1 change: 0 additions & 1 deletion public/next.svg

This file was deleted.

1 change: 0 additions & 1 deletion public/thirteen.svg

This file was deleted.

1 change: 0 additions & 1 deletion public/vercel.svg

This file was deleted.

33 changes: 33 additions & 0 deletions utils/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import { google } from "googleapis";

export const getFileList = async () => {
const client = new google.auth.JWT({
email: process.env.CLIENT_EMAIL,
scopes: [
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/drive",
],
key: process.env.PRIVATE_KEY,
});

await client.authorize();

// fetch the entire folder
const folderId = "11N3RY-5t73GvWfvXa-qFAvww8vIw1AOG";
const gsapi2 = google.drive({ version: "v3", auth: client });
const opt2 = {
q: `'${folderId}' in parents and trashed = false`,
};
let data2 = await gsapi2.files.list(opt2);
const fileList = data2.data.files
?.sort((a, b) => {
if (a.name && b.name) {
if (a?.name < b?.name) return -1;
if (a?.name > b?.name) return 1;
}
return 0;
})
.filter((item) => !item.mimeType.includes("folder"));
return [fileList, client];
}
File renamed without changes.
6 changes: 6 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"crons": [{
"path": "/api/cron",
"schedule": "30 * * * *"
}]
}