-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from iamvishnusankar/dynamic-sitemap-support
Dynamic sitemap support
- Loading branch information
Showing
17 changed files
with
224 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: 1.3$(rev:.r) | ||
name: 1.4$(rev:.r) | ||
trigger: | ||
branches: | ||
include: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* 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(), | ||
// changefreq | ||
// priority | ||
}, | ||
{ | ||
loc: 'https://example.com/dynamic-path-2', | ||
lastmod: new Date().toISOString(), | ||
// changefreq | ||
// priority | ||
}, | ||
]) | ||
} | ||
|
||
// Default export to prevent next.js errors | ||
export default () => {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#!/usr/bin/env node | ||
require('../dist/cjs') | ||
require('../dist/cjs/cli') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { loadConfig, getRuntimeConfig, updateConfig } from './config' | ||
import { loadManifest } from './manifest' | ||
import { createUrlSet, generateUrl } from './url' | ||
import { generateSitemap } from './sitemap/generateSitemap' | ||
import { toChunks } from './array' | ||
import { | ||
resolveSitemapChunks, | ||
getRuntimePaths, | ||
getConfigFilePath, | ||
} from './path' | ||
import { exportRobotsTxt } from './robots-txt' | ||
|
||
// Get config file path | ||
const configFilePath = getConfigFilePath() | ||
|
||
// Load next-sitemap.js | ||
let config = loadConfig(configFilePath) | ||
|
||
// Get runtime paths | ||
const runtimePaths = getRuntimePaths(config) | ||
|
||
// get runtime config | ||
const runtimeConfig = getRuntimeConfig(runtimePaths) | ||
|
||
// Update config with runtime config | ||
config = updateConfig(config, runtimeConfig) | ||
|
||
// Load next.js manifest files | ||
const manifest = loadManifest(runtimePaths) | ||
|
||
// Create url-set based on config and manifest | ||
const urlSet = createUrlSet(config, manifest) | ||
|
||
// Split sitemap into multiple files | ||
const chunks = toChunks(urlSet, config.sitemapSize!) | ||
const sitemapChunks = resolveSitemapChunks(runtimePaths.SITEMAP_FILE, chunks) | ||
|
||
// All sitemaps array to keep track of generated sitemap files. | ||
// Later to be added on robots.txt | ||
const allSitemaps: string[] = [] | ||
|
||
// Generate sitemaps from chunks | ||
sitemapChunks.forEach((chunk) => { | ||
generateSitemap(chunk) | ||
allSitemaps.push(generateUrl(config.siteUrl, `/${chunk.filename}`)) | ||
}) | ||
|
||
// Generate robots.txt | ||
if (config.generateRobotsTxt) { | ||
exportRobotsTxt(runtimePaths, config, allSitemaps) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/next-sitemap/src/dynamic-sitemap/getServerSideSitemap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './getServerSideSitemap' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,2 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { loadConfig, getRuntimeConfig, updateConfig } from './config' | ||
import { loadManifest } from './manifest' | ||
import { createUrlSet, generateUrl } from './url' | ||
import { generateSitemap } from './sitemap' | ||
import { toChunks } from './array' | ||
import { | ||
resolveSitemapChunks, | ||
getRuntimePaths, | ||
getConfigFilePath, | ||
} from './path' | ||
import { exportRobotsTxt } from './robots-txt' | ||
|
||
// Get config file path | ||
const configFilePath = getConfigFilePath() | ||
|
||
// Load next-sitemap.js | ||
let config = loadConfig(configFilePath) | ||
|
||
// Get runtime paths | ||
const runtimePaths = getRuntimePaths(config) | ||
|
||
// get runtime config | ||
const runtimeConfig = getRuntimeConfig(runtimePaths) | ||
|
||
// Update config with runtime config | ||
config = updateConfig(config, runtimeConfig) | ||
|
||
// Load next.js manifest files | ||
const manifest = loadManifest(runtimePaths) | ||
|
||
// Create url-set based on config and manifest | ||
const urlSet = createUrlSet(config, manifest) | ||
|
||
// Split sitemap into multiple files | ||
const chunks = toChunks(urlSet, config.sitemapSize!) | ||
const sitemapChunks = resolveSitemapChunks(runtimePaths.SITEMAP_FILE, chunks) | ||
|
||
// All sitemaps array to keep track of generated sitemap files. | ||
// Later to be added on robots.txt | ||
const allSitemaps: string[] = [] | ||
|
||
// Generate sitemaps from chunks | ||
sitemapChunks.forEach((chunk) => { | ||
generateSitemap(config, chunk.path, chunk.fields) | ||
allSitemaps.push(generateUrl(config.siteUrl, `/${chunk.filename}`)) | ||
}) | ||
|
||
// Generate robots.txt | ||
if (config.generateRobotsTxt) { | ||
exportRobotsTxt(runtimePaths, config, allSitemaps) | ||
} | ||
export * from './sitemap/buildSitemapXml' | ||
export * from './dynamic-sitemap' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ISitemapFiled } from '../interface' | ||
import { withXMLTemplate } from './withXMLTemplate' | ||
|
||
export const buildSitemapXml = (fields: ISitemapFiled[]): string => { | ||
const content = fields.reduce((prev, curr) => { | ||
let field = '' | ||
for (const key of Object.keys(curr)) { | ||
field += `<${key}>${curr[key]}</${key}>` | ||
} | ||
|
||
// Append previous value and return | ||
return `${prev}<url>${field}</url>\n` | ||
}, '') | ||
|
||
return withXMLTemplate(content) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ISitemapChunk } from '../interface' | ||
import { exportFile } from '../file' | ||
import { buildSitemapXml } from './buildSitemapXml' | ||
|
||
export const generateSitemap = (chunk: ISitemapChunk): void => { | ||
const sitemapXml = buildSitemapXml(chunk.fields) | ||
exportFile(chunk.path, sitemapXml) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
|
||
export const withXMLTemplate = (content: string): string => { | ||
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">\n${content}</urlset>` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.