Skip to content

Commit

Permalink
generate sitemap dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaqiuxy committed Jul 11, 2020
1 parent 4680fcc commit 19c7581
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"react-waypoint": "^9.0.2",
"redoc": "^2.0.0-rc.29",
"rtl-detect": "^1.0.2",
"sitemap": "^2.2.0",
"sitemap": "^6.1.7",
"statuses": "^2.0.0",
"string-score": "^1.0.1",
"styled-components": "^5.1.0",
Expand Down
62 changes: 46 additions & 16 deletions src/lib/sitemap.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
import { createSitemap } from 'sitemap';
import { promisify } from 'util';
import fetch from 'cross-fetch';
import statuses from 'statuses';
import { SitemapStream, streamToPromise } from 'sitemap';

const sm = createSitemap({
hostname: 'https://ndaify.com',
});
const sitemap = () => async (ctx) => {
try {
const sm = new SitemapStream({
hostname: process.env.CANONICAL_URL,
lastmodDateOnly: false,
xmlns: {
news: true,
xhtml: true,
image: true,
video: true,
},
});

const toXML = promisify(sm.toXML).bind(sm);
sm.write({
url: '/',
changefreq: 'weekly',
priority: 1,
img: [
{
url: 'https://ndaify.com/images/meta.png',
caption: 'NDAify helps you keep your secrets under wraps.',
title: 'NDAify',
},
],
});

// See https://github.com/ekalinin/sitemap.js
sm.add({
url: '/',
changefreq: 'weekly',
priority: 1,
});
// const ndaifyService = new NdaifyService();
// const ndaTemplateOptions = await ndaifyService.getNdaTemplateOptions();
const response = await fetch(`${process.env.NDAIFY_ENDPOINT_URL}/nda-templates/options`, {
method: 'GET',
});

const { ndaTemplateOptions } = await response.json();

ndaTemplateOptions.forEach((opt) => {
sm.write({
url: `/sample/${opt.ndaTemplateId}`,
changefreq: 'weekly',
priority: 1,
});
});

sm.end();

const xml = await streamToPromise(sm);

const sitemap = () => async (ctx) => {
try {
const SITEMAP_XML = await toXML();
ctx.set('Content-Type', 'text/xml');
ctx.res.write(SITEMAP_XML);
ctx.res.write(xml.toString());
} catch {
ctx.status = statuses('Internal Server Error');
} finally {
Expand Down
39 changes: 25 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.9.tgz#79df4ae965fb76d31943b54a6419599307a21394"
integrity sha512-EPZBIGed5gNnfWCiwEIwTE2Jdg4813odnG8iNPMQGrqVxrI+wL68SPtPeCX+ZxGBaA6pKAVc6jaKgP/Q0QzfdQ==

"@types/node@^14.0.14":
version "14.0.14"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.14.tgz#24a0b5959f16ac141aeb0c5b3cd7a15b7c64cbce"
integrity sha512-syUgf67ZQpaJj01/tRTknkMNoBBLWJOBODF0Zm4NrXmiSuxjymFrxnTu1QVYRubhVkRcZLYZG8STTwJRdVm/WQ==

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
Expand Down Expand Up @@ -1833,6 +1838,13 @@
"@types/prop-types" "*"
csstype "^2.2.0"

"@types/sax@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@types/sax/-/sax-1.2.1.tgz#e0248be936ece791a82db1a57f3fb5f7c87e8172"
integrity sha512-dqYdvN7Sbw8QT/0Ci5rhjE4/iCMJEM0Y9rHpCu+gGXD9Lwbz28t6HI2yegsB6BoV1sShRMU6lAmAcgRjmFy7LA==
dependencies:
"@types/node" "*"

"@types/schema-utils@^2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@types/schema-utils/-/schema-utils-2.4.0.tgz#9983012045d541dcee053e685a27c9c87c840fcd"
Expand Down Expand Up @@ -2231,6 +2243,11 @@ are-we-there-yet@~1.1.2:
delegates "^1.0.0"
readable-stream "^2.0.6"

arg@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==

argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
Expand Down Expand Up @@ -9614,13 +9631,15 @@ sisteransi@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.0.tgz#77d9622ff909080f1c19e5f4a1df0c1b0a27b88c"

sitemap@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/sitemap/-/sitemap-2.2.0.tgz#98b8502762c5d7e8c77c9be5061dce85b326f1b0"
sitemap@^6.1.7:
version "6.1.7"
resolved "https://registry.yarnpkg.com/sitemap/-/sitemap-6.1.7.tgz#136141415dbaaf89e114322bb63510ff0e0d6b6a"
integrity sha512-cbomwRGX4AY2zJsa7RKAMANGMtGIrmL4NWXJGIR951v23fQtQD8Rh38xFHmjI/hEiVzUoEPrgmHXdUgOBy4tBQ==
dependencies:
lodash "^4.17.10"
url-join "^4.0.0"
xmlbuilder "^10.0.0"
"@types/node" "^14.0.14"
"@types/sax" "^1.2.1"
arg "^4.1.3"
sax "^1.2.4"

slash@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -10650,10 +10669,6 @@ urix@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"

url-join@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/url-join/-/url-join-4.0.1.tgz#b642e21a2646808ffa178c4c5fda39844e12cde7"

url-loader@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/url-loader/-/url-loader-4.1.0.tgz#c7d6b0d6b0fccd51ab3ffc58a78d32b8d89a7be2"
Expand Down Expand Up @@ -11030,10 +11045,6 @@ xml-name-validator@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a"

xmlbuilder@^10.0.0:
version "10.1.1"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-10.1.1.tgz#8cae6688cc9b38d850b7c8d3c0a4161dcaf475b0"

xmlchars@^2.1.1:
version "2.2.0"
resolved "https://registry.yarnpkg.com/xmlchars/-/xmlchars-2.2.0.tgz#060fe1bcb7f9c76fe2a17db86a9bc3ab894210cb"
Expand Down

0 comments on commit 19c7581

Please sign in to comment.