From 7a6f240ce249a04909acd467898acc8f93b35824 Mon Sep 17 00:00:00 2001 From: Daniele Bertella Date: Mon, 16 Nov 2020 11:58:40 +0000 Subject: [PATCH 1/3] Add script to generate sitemap.xml at build time --- generateSitemap.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 generateSitemap.js diff --git a/generateSitemap.js b/generateSitemap.js new file mode 100644 index 0000000..9b5e787 --- /dev/null +++ b/generateSitemap.js @@ -0,0 +1,58 @@ +const fs = require("fs"); +const globby = require("globby"); +const path = require("path"); + +const { + getAllPostsWithSlug, + getAllProductCategoriesWithSlug, + getAllProductsWithSlug, +} = require("./lib"); // this doesn't work because typescript and/or es6 modules MODULE_NOT_FOUND + +// const getAllPostsWithSlug = require("./lib/news"); + +const lambdaPagesFilePath = path.join(__dirname, ".", "lib", "sitemap.xml"); + +const createSitemap = (routes, host) => ` + + ${routes + .map((route) => `https://cascinarampina.it${route}`) + .join("")} + `; + +(async () => { + const globbs = await globby([ + "pages/**/*.tsx", + "!pages/api", + "!pages/**/\\[*\\].tsx", + "!pages/**/*.xml.tsx", + ]); + const pages = globbs + .map((route) => + route.replace("pages", "").replace(".tsx", "").replace("/index", "") + ) + .sort(); + + const news = (await getAllPostsWithSlug()).edges.map( + ({ node }) => `/news/${node.slug}` + ); + + const productCategories = (await getAllProductCategoriesWithSlug()).edges.map( + ({ node }) => `/categorie-prodotti/${node.slug}` + ); + + const products = (await getAllProductsWithSlug()).edges.map( + ({ node }) => `/prodotti/${node.slug}` + ); + + const sitemap = createSitemap( + [...pages, ...products, ...productCategories, ...news], + req.headers.host + ); + + fs.writeFileSync(lambdaPagesFilePath, JSON.stringify(pages, null, 2), { + encoding: "utf-8", + }); + console.log(`Built ${lambdaPagesFilePath}`); + + process.exit(0); +})(); From bce93ef1a164b358a488a9da90cccb2ac6ef4bfb Mon Sep 17 00:00:00 2001 From: nkint Date: Mon, 16 Nov 2020 22:05:42 +0100 Subject: [PATCH 2/3] feat(static map): generate static map locally by ts-node --- README.md | 15 +++++++++++++++ generateStatic.js => generateStatic.ts | 8 ++++---- node.tsconfig.json | 25 +++++++++++++++++++++++++ package.json | 5 +++-- yarn.lock | 8 ++++---- 5 files changed, 51 insertions(+), 10 deletions(-) rename generateStatic.js => generateStatic.ts (73%) create mode 100644 node.tsconfig.json diff --git a/README.md b/README.md index 48e58ac..f95f343 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,21 @@ yarn dev Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. +## Install node & typescript to build static map locally + +```bash +npm i -g typescript +npm i -g ts-node +``` + +Pay attention to the globally installed typescript (can be in peerDependencies?). + +To execute: + +```bash +yarn static-map +``` + ## Powered by Worppress I'm using wordpress as an headless CMS using [WPGraphQL](https://github.com/wp-graphql) plugin, to have a graphql server instead of plain rest apis diff --git a/generateStatic.js b/generateStatic.ts similarity index 73% rename from generateStatic.js rename to generateStatic.ts index 3d21293..c65c00e 100644 --- a/generateStatic.js +++ b/generateStatic.ts @@ -1,8 +1,8 @@ -const fs = require("fs"); -const globby = require("globby"); -const path = require("path"); +import * as fs from "fs"; +import * as path from "path"; +import globby from "globby"; -const lambdaPagesFilePath = path.join(__dirname, '.', 'lib', 'pages.json'); +const lambdaPagesFilePath = path.join(__dirname, ".", "lib", "pages.json"); (async () => { const pages = await globby([ diff --git a/node.tsconfig.json b/node.tsconfig.json new file mode 100644 index 0000000..15bf6ff --- /dev/null +++ b/node.tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "esModuleInterop": true, + "module": "commonjs", + "target": "esnext", + "jsx": "react", + "sourceMap": false, + "experimentalDecorators": true, + "noImplicitUseStrict": true, + "removeComments": true, + "moduleResolution": "node", + "lib": [ + "es2017", + "dom" + ], + "typeRoots": [ + "node_modules/@types" + ] + }, + "exclude": [ + "node_modules", + "out", + ".next" + ] +} \ No newline at end of file diff --git a/package.json b/package.json index fb8ffd8..9e60375 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "dev": "next dev", "prebuild": "node generateStatic", "build": "next build", - "start": "next start" + "start": "next start", + "static-map": "ts-node --project node.tsconfig.json ./generateStatic.ts" }, "dependencies": { "-": "^0.0.1", @@ -24,7 +25,7 @@ "theme-ui": "^0.3.1" }, "devDependencies": { - "@types/node": "^14.14.6", + "@types/node": "^14.14.7", "@types/react": "^16.9.55", "@types/react-image-gallery": "^1.0.0", "@types/theme-ui": "^0.3.7", diff --git a/yarn.lock b/yarn.lock index fb95c99..49195ce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1431,10 +1431,10 @@ resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.164.tgz#52348bcf909ac7b4c1bcbeda5c23135176e5dfa0" integrity sha512-fXCEmONnrtbYUc5014avwBeMdhHHO8YJCkOBflUL9EoJBSKZ1dei+VO74fA7JkTHZ1GvZack2TyIw5U+1lT8jg== -"@types/node@^14.14.6": - version "14.14.6" - resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.6.tgz#146d3da57b3c636cc0d1769396ce1cfa8991147f" - integrity sha512-6QlRuqsQ/Ox/aJEQWBEJG7A9+u7oSYl3mem/K8IzxXG/kAGbV1YPD9Bg9Zw3vyxC/YP+zONKwy8hGkSt1jxFMw== +"@types/node@^14.14.7": + version "14.14.7" + resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.7.tgz#8ea1e8f8eae2430cf440564b98c6dfce1ec5945d" + integrity sha512-Zw1vhUSQZYw+7u5dAwNbIA9TuTotpzY/OF7sJM9FqPOF3SPjKnxrjoTktXDZgUjybf4cWVBP7O8wvKdSaGHweg== "@types/parse-json@^4.0.0": version "4.0.0" From 35de9166fc53dd2911fc6817fbaee431e91a8bff Mon Sep 17 00:00:00 2001 From: nkint Date: Tue, 17 Nov 2020 10:43:26 +0100 Subject: [PATCH 3/3] refactor(sitemap): moved sitemap from a standalone node script to next.js api --- README.md | 15 ---------- generateSitemap.js | 58 ------------------------------------ generateStatic.ts | 25 ---------------- node.tsconfig.json | 1 + package.json | 6 ++-- pages/api/hello.js | 6 ---- pages/api/sitemap.ts | 71 ++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 59 ++++++++++++++++++++++++++++-------- 8 files changed, 121 insertions(+), 120 deletions(-) delete mode 100644 generateSitemap.js delete mode 100644 generateStatic.ts delete mode 100644 pages/api/hello.js create mode 100644 pages/api/sitemap.ts diff --git a/README.md b/README.md index f95f343..48e58ac 100644 --- a/README.md +++ b/README.md @@ -12,21 +12,6 @@ yarn dev Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. -## Install node & typescript to build static map locally - -```bash -npm i -g typescript -npm i -g ts-node -``` - -Pay attention to the globally installed typescript (can be in peerDependencies?). - -To execute: - -```bash -yarn static-map -``` - ## Powered by Worppress I'm using wordpress as an headless CMS using [WPGraphQL](https://github.com/wp-graphql) plugin, to have a graphql server instead of plain rest apis diff --git a/generateSitemap.js b/generateSitemap.js deleted file mode 100644 index 9b5e787..0000000 --- a/generateSitemap.js +++ /dev/null @@ -1,58 +0,0 @@ -const fs = require("fs"); -const globby = require("globby"); -const path = require("path"); - -const { - getAllPostsWithSlug, - getAllProductCategoriesWithSlug, - getAllProductsWithSlug, -} = require("./lib"); // this doesn't work because typescript and/or es6 modules MODULE_NOT_FOUND - -// const getAllPostsWithSlug = require("./lib/news"); - -const lambdaPagesFilePath = path.join(__dirname, ".", "lib", "sitemap.xml"); - -const createSitemap = (routes, host) => ` - - ${routes - .map((route) => `https://cascinarampina.it${route}`) - .join("")} - `; - -(async () => { - const globbs = await globby([ - "pages/**/*.tsx", - "!pages/api", - "!pages/**/\\[*\\].tsx", - "!pages/**/*.xml.tsx", - ]); - const pages = globbs - .map((route) => - route.replace("pages", "").replace(".tsx", "").replace("/index", "") - ) - .sort(); - - const news = (await getAllPostsWithSlug()).edges.map( - ({ node }) => `/news/${node.slug}` - ); - - const productCategories = (await getAllProductCategoriesWithSlug()).edges.map( - ({ node }) => `/categorie-prodotti/${node.slug}` - ); - - const products = (await getAllProductsWithSlug()).edges.map( - ({ node }) => `/prodotti/${node.slug}` - ); - - const sitemap = createSitemap( - [...pages, ...products, ...productCategories, ...news], - req.headers.host - ); - - fs.writeFileSync(lambdaPagesFilePath, JSON.stringify(pages, null, 2), { - encoding: "utf-8", - }); - console.log(`Built ${lambdaPagesFilePath}`); - - process.exit(0); -})(); diff --git a/generateStatic.ts b/generateStatic.ts deleted file mode 100644 index c65c00e..0000000 --- a/generateStatic.ts +++ /dev/null @@ -1,25 +0,0 @@ -import * as fs from "fs"; -import * as path from "path"; -import globby from "globby"; - -const lambdaPagesFilePath = path.join(__dirname, ".", "lib", "pages.json"); - -(async () => { - const pages = await globby([ - "pages/**/*.tsx", - "!pages/api", - "!pages/**/\\[*\\].tsx", - "!pages/**/*.xml.tsx", - ]).then((all) => - all.map((route) => - route.replace("pages", "").replace(".tsx", "").replace("/index", "") - ) - ); - - fs.writeFileSync(lambdaPagesFilePath, JSON.stringify(pages, null, 2), { - encoding: "utf-8", - }); - console.log(`Built ${lambdaPagesFilePath}`); - - process.exit(0); -})(); diff --git a/node.tsconfig.json b/node.tsconfig.json index 15bf6ff..59d54f0 100644 --- a/node.tsconfig.json +++ b/node.tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "baseUrl": ".", "esModuleInterop": true, "module": "commonjs", "target": "esnext", diff --git a/package.json b/package.json index 9e60375..27927c8 100644 --- a/package.json +++ b/package.json @@ -6,13 +6,11 @@ "dev": "next dev", "prebuild": "node generateStatic", "build": "next build", - "start": "next start", - "static-map": "ts-node --project node.tsconfig.json ./generateStatic.ts" + "start": "next start" }, "dependencies": { - "-": "^0.0.1", "@types/lodash": "^4.14.164", - "D": "^1.0.0", + "@vercel/node": "^1.8.5", "dayjs": "^1.9.5", "graphql-tag": "^2.11.0", "immer": "^7.0.14", diff --git a/pages/api/hello.js b/pages/api/hello.js deleted file mode 100644 index 5b77ec0..0000000 --- a/pages/api/hello.js +++ /dev/null @@ -1,6 +0,0 @@ -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction - -export default (req, res) => { - res.statusCode = 200 - res.json({ name: 'John Doe' }) -} diff --git a/pages/api/sitemap.ts b/pages/api/sitemap.ts new file mode 100644 index 0000000..38062e9 --- /dev/null +++ b/pages/api/sitemap.ts @@ -0,0 +1,71 @@ +import * as path from "path"; +import globby from "globby"; + +import { NextApiRequest, NextApiResponse } from "next"; +import { + getAllPostsWithSlug, + getAllProductCategoriesWithSlug, + getAllProductsWithSlug, +} from "lib"; + +type EdgeSlug = { + edges: { node: { slug: string } }[]; +}; + +const lambdaPagesFilePath = path.join(__dirname, ".", "lib", "sitemap.xml"); + +const createSitemap = ( + routes: string[] +) => ` + + ${routes + .map((route) => `https://cascinarampina.it${route}`) + .join("")} + `; + +async function getSlugsFromNamedApi(api: () => Promise, name: string) { + const newsEdge = (await api()) as EdgeSlug; + return newsEdge.edges.map(({ node }: any) => `${name}${node.slug}`); +} + +async function fetchPages() { + const globbs = await globby([ + "pages/**/*.tsx", + "!pages/api", + "!pages/**/\\[*\\].tsx", + "!pages/**/*.xml.tsx", + ]); + + const pages = globbs + .map((route) => + route.replace("pages", "").replace(".tsx", "").replace("/index", "") + ) + .sort(); + + const news = await getSlugsFromNamedApi(getAllPostsWithSlug, `/news/`); + const productCategories = await getSlugsFromNamedApi( + getAllProductCategoriesWithSlug, + `/categorie-prodotti/` + ); + const products = await getSlugsFromNamedApi( + getAllProductsWithSlug, + `/prodotti/` + ); + + const sitemap = createSitemap([ + ...pages, + ...products, + ...productCategories, + ...news, + ]); + + console.log(`Built ${lambdaPagesFilePath}`); + + return sitemap; +} + +export default async (_: NextApiRequest, res: NextApiResponse) => { + const siteMap = await fetchPages(); + res.setHeader("Content-Type", "application/xml"); + res.status(200).send(siteMap); +}; diff --git a/yarn.lock b/yarn.lock index 49195ce..90d38eb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,11 +2,6 @@ # yarn lockfile v1 -"-@^0.0.1": - version "0.0.1" - resolved "https://registry.yarnpkg.com/-/-/--0.0.1.tgz#db6db7cd866142880dd03e5b8781d1b4fac0e5bd" - integrity sha512-3HfneK3DGAm05fpyj20sT3apkNcvPpCuccOThOPdzz8sY7GgQGe0l93XH9bt+YzibcTIgUAIMoyVJI740RtgyQ== - "@ampproject/toolbox-core@^2.6.0": version "2.6.1" resolved "https://registry.yarnpkg.com/@ampproject/toolbox-core/-/toolbox-core-2.6.1.tgz#af97ec253bf39e5fe5121b8ec28f1f35d1878446" @@ -1431,7 +1426,7 @@ resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.164.tgz#52348bcf909ac7b4c1bcbeda5c23135176e5dfa0" integrity sha512-fXCEmONnrtbYUc5014avwBeMdhHHO8YJCkOBflUL9EoJBSKZ1dei+VO74fA7JkTHZ1GvZack2TyIw5U+1lT8jg== -"@types/node@^14.14.7": +"@types/node@*", "@types/node@^14.14.7": version "14.14.7" resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.7.tgz#8ea1e8f8eae2430cf440564b98c6dfce1ec5945d" integrity sha512-Zw1vhUSQZYw+7u5dAwNbIA9TuTotpzY/OF7sJM9FqPOF3SPjKnxrjoTktXDZgUjybf4cWVBP7O8wvKdSaGHweg== @@ -1498,6 +1493,15 @@ "@types/styled-system" "*" "@types/theme-ui" "*" +"@vercel/node@^1.8.5": + version "1.8.5" + resolved "https://registry.yarnpkg.com/@vercel/node/-/node-1.8.5.tgz#2c8b9532f1bb25734a9964c52973386ed78022d4" + integrity sha512-1iw7FSR8Oau6vZB1MWfBnA5q2a/IqRHiSZSbt8lz0dyTF599q8pc5GcSv/TvmrYaEGzh3+N0S4cbmuMCqVlwJg== + dependencies: + "@types/node" "*" + ts-node "8.9.1" + typescript "3.9.3" + "@webassemblyjs/ast@1.9.0": version "1.9.0" resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.9.0.tgz#bd850604b4042459a5a41cd7d338cbed695ed964" @@ -1653,11 +1657,6 @@ resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d" integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ== -D@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/D/-/D-1.0.0.tgz#c348a4e034f72847be51206fc530fc089e9cc2a9" - integrity sha512-nQvrCBu7K2pSSEtIM0EEF03FVjcczCXInMt3moLNFbjlWx6bZrX72uT6/1uAXDbnzGUAx9gTyDiQ+vrFi663oA== - abort-controller@3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392" @@ -1784,6 +1783,11 @@ are-we-there-yet@~1.1.2: delegates "^1.0.0" readable-stream "^2.0.6" +arg@^4.1.0: + version "4.1.3" + resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089" + integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA== + arity-n@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/arity-n/-/arity-n-1.0.4.tgz#d9e76b11733e08569c0847ae7b39b2860b30b745" @@ -2727,6 +2731,11 @@ detect-libc@^1.0.3: resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b" integrity sha1-+hN8S9aY7fVc1c0CrFWfkaTEups= +diff@^4.0.1: + version "4.0.2" + resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d" + integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A== + diffie-hellman@^5.0.0: version "5.0.3" resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.3.tgz#40e8ee98f55a2149607146921c63e1ae5f3d2875" @@ -3920,6 +3929,11 @@ make-dir@^3.0.2: dependencies: semver "^6.0.0" +make-error@^1.1.1: + version "1.3.6" + resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2" + integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw== + map-cache@^0.2.2: version "0.2.2" resolved "https://registry.yarnpkg.com/map-cache/-/map-cache-0.2.2.tgz#c32abd0bd6525d9b051645bb4f26ac5dc98a0dbf" @@ -5315,7 +5329,7 @@ source-map-resolve@^0.5.0, source-map-resolve@^0.5.2: source-map-url "^0.4.0" urix "^0.1.0" -source-map-support@~0.5.12: +source-map-support@^0.5.17, source-map-support@~0.5.12: version "0.5.19" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61" integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw== @@ -5728,6 +5742,17 @@ traverse@0.6.6: resolved "https://registry.yarnpkg.com/traverse/-/traverse-0.6.6.tgz#cbdf560fd7b9af632502fed40f918c157ea97137" integrity sha1-y99WD9e5r2MlAv7UD5GMFX6pcTc= +ts-node@8.9.1: + version "8.9.1" + resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.9.1.tgz#2f857f46c47e91dcd28a14e052482eb14cfd65a5" + integrity sha512-yrq6ODsxEFTLz0R3BX2myf0WBCSQh9A+py8PBo1dCzWIOcvisbyH6akNKqDHMgXePF2kir5mm5JXJTH3OUJYOQ== + dependencies: + arg "^4.1.0" + diff "^4.0.1" + make-error "^1.1.1" + source-map-support "^0.5.17" + yn "3.1.1" + ts-pnp@^1.1.6: version "1.2.0" resolved "https://registry.yarnpkg.com/ts-pnp/-/ts-pnp-1.2.0.tgz#a500ad084b0798f1c3071af391e65912c86bca92" @@ -5770,6 +5795,11 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= +typescript@3.9.3: + version "3.9.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.3.tgz#d3ac8883a97c26139e42df5e93eeece33d610b8a" + integrity sha512-D/wqnB2xzNFIcoBG9FG8cXRDjiqSTbG2wd8DMZeQyJlP1vfTkIxH4GKveWaEBYySKIg+USu+E+EDIR47SqnaMQ== + typescript@^4.0.5: version "4.0.5" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389" @@ -6026,3 +6056,8 @@ yaml@^1.7.2: version "1.10.0" resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e" integrity sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg== + +yn@3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50" + integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==