Skip to content

Commit

Permalink
- Added dynamic-sitemap-example
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvishnusankar committed Jan 10, 2021
1 parent 30d2b36 commit 9035387
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 9 deletions.
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"postbuild": "next-sitemap"
},
"dependencies": {
"@types/react-dom": "^17.0.0",
"next": "^10.0.4",
"react": "^17.0.1",
"react-dom": "^17.0.1"
Expand Down
23 changes: 23 additions & 0 deletions example/pages/server-sitemap.xml/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-empty-function */
import { getServerSideSitemap } from 'next-sitemap'
import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (ctx) => {
// Method to source urls from cms
// const urls = await fetch('https//example.com/api')

return getServerSideSitemap(ctx, [
{
loc: 'https://example.com',
lastmod: new Date().toISOString(),
},
{
loc: 'https://example.com/dynamic-path-2',
lastmod: new Date().toISOString(),
},
])
}

// Default export to prevent next.js errors
export default () => {}
9 changes: 6 additions & 3 deletions packages/next-sitemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "Sitemap generator for next.js",
"main": "dist/cjs/index.js",
"module": "dist/esnext/index.js",
"module": "dist/esm/index.js",
"types": "dist/@types/index.d.ts",
"repository": "https://github.com/iamvishnusankar/next-sitemap.git",
"author": "Vishnu Sankar (@iamvishnusankar)",
Expand All @@ -16,12 +16,15 @@
},
"scripts": {
"lint": "tsc --noEmit --declaration",
"build": "tsc && yarn build:esnext",
"build:esnext": "tsc --module esnext --outDir dist/esnext"
"build": "tsc && yarn build:esm",
"build:esm": "tsc --module es2015 --outDir dist/esm"
},
"dependencies": {
"@corex/deepmerge": "^2.5.3",
"matcher": "^3.0.0",
"minimist": "^1.2.5"
},
"peerDependencies": {
"next": "*"
}
}
6 changes: 3 additions & 3 deletions packages/next-sitemap/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export const transformSitemap = (
): ISitemapFiled => {
return {
loc: url,
changefreq: config.changefreq,
priority: config.priority,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
changefreq: config?.changefreq,
priority: config?.priority,
lastmod: config?.autoLastmod ? new Date().toISOString() : undefined,
}
}

Expand Down
28 changes: 28 additions & 0 deletions packages/next-sitemap/src/dynamic-sitemap/getServerSideSitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { ISitemapFiled } from '../interface'
import { buildSitemapXml } from '../sitemap/buildSitemapXml'

export const getServerSideSitemap = async (
context: import('next').GetServerSidePropsContext,
fields: ISitemapFiled[]
) => {
const sitemapContent = buildSitemapXml(fields)

if (context && context.res) {
const { res } = context

// Set header
res.setHeader('Content-Type', 'text/xml')

// Write the sitemap context to resonse
res.write(sitemapContent)

// End response
res.end()
}

// Empty props
return {
props: {},
}
}
1 change: 1 addition & 0 deletions packages/next-sitemap/src/dynamic-sitemap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './getServerSideSitemap'
1 change: 1 addition & 0 deletions packages/next-sitemap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './sitemap/buildSitemapXml'
export * from './dynamic-sitemap'
6 changes: 4 additions & 2 deletions packages/next-sitemap/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"rootDir": "src",
"outDir": "dist/cjs",
"declarationDir": "dist/@types",
"module": "CommonJS"
"module": "CommonJS",
"target": "ES2015"
},
"include": ["src"]
"include": ["src"],
"exclude": ["node_modules"]
}
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,14 @@
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7"
integrity sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw==

"@types/react@^17.0.0":
"@types/react-dom@^17.0.0":
version "17.0.0"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-17.0.0.tgz#b3b691eb956c4b3401777ee67b900cb28415d95a"
integrity sha512-lUqY7OlkF/RbNtD5nIq7ot8NquXrdFrjSOR6+w9a9RFQevGi1oZO1dcJbXMeONAPKtZ2UrZOEJ5UOCVsxbLk/g==
dependencies:
"@types/react" "*"

"@types/react@*", "@types/react@^17.0.0":
version "17.0.0"
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.0.tgz#5af3eb7fad2807092f0046a1302b7823e27919b8"
integrity sha512-aj/L7RIMsRlWML3YB6KZiXB3fV2t41+5RBGYF8z+tAKU43Px8C3cYUZsDvf1/+Bm4FK21QWBrDutu8ZJ/70qOw==
Expand Down

0 comments on commit 9035387

Please sign in to comment.