Skip to content

Commit

Permalink
[WIP] Static export
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvishnusankar committed May 18, 2023
1 parent dd7bd29 commit 706e1d3
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 11 deletions.
11 changes: 11 additions & 0 deletions examples/static-export/next-sitemap.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const nextConfig = require('./next.config')

/** @type {import('next-sitemap').IConfig} */
const config = {
...nextConfig,
siteUrl: process.env.SITE_URL || 'https://example.com',
generateRobotsTxt: true,
}

module.exports = config
2 changes: 1 addition & 1 deletion examples/static-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"build": "next build && next-sitemap",
"start": "next start",
"lint": "next lint"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/next-sitemap/src/builders/url-set-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ export class UrlSetBuilder {

// Init all page keys
const allKeys = [
...Object.keys(this.manifest?.build.pages),
...Object.keys(this.manifest?.build?.pages ?? {}),
...(this.manifest?.build?.ampFirstPages ?? []),
...(this.manifest?.preRender
? Object.keys(this.manifest?.preRender.routes)
? Object.keys(this.manifest?.preRender?.routes ?? {})
: []),
]

Expand Down
5 changes: 4 additions & 1 deletion packages/next-sitemap/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export class CLI {
const { config, runtimePaths } = await new ConfigParser().loadConfig()

// Load next.js manifest
const manifest = await new ManifestParser().loadManifest(runtimePaths)
const manifest = await new ManifestParser().loadManifest(
config,
runtimePaths
)

// Generate url set
const urlSet = await new UrlSetBuilder(config, manifest).createUrlSet()
Expand Down
6 changes: 6 additions & 0 deletions packages/next-sitemap/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ export interface IConfig {
*/
changefreq?: Changefreq

/**
* The type of build output.
* @see https://nextjs.org/docs/pages/api-reference/next-config-js/output
*/
output: 'standalone' | 'export' | undefined

/**
* Priority
* @default 0.7
Expand Down
9 changes: 8 additions & 1 deletion packages/next-sitemap/src/parsers/config-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ export class ConfigParser {
* @returns
*/
private async getRuntimeConfig(
config: IConfig,
runtimePaths: IRuntimePaths
): Promise<Partial<IConfig>> {
// Skip export marker loading if output is set to "export"
if (config?.output === 'export') {
return {}
}

// Load export marker
const exportMarkerConfig = await loadJSON<IExportMarker>(
runtimePaths.EXPORT_MARKER,
false
Expand All @@ -38,7 +45,7 @@ export class ConfigParser {
runtimePaths: IRuntimePaths
): Promise<IConfig> {
// Runtime configs
const runtimeConfig = await this.getRuntimeConfig(runtimePaths)
const runtimeConfig = await this.getRuntimeConfig(config, runtimePaths)

// Prioritize `trailingSlash` value from `next-sitemap.js`
const trailingSlashConfig: Partial<IConfig> = {}
Expand Down
16 changes: 12 additions & 4 deletions packages/next-sitemap/src/parsers/manifest-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ import type {
IBuildManifest,
IRuntimePaths,
IRoutesManifest,
IConfig,
} from '../interface.js'
import { loadJSON } from '../utils/file.js'

export class ManifestParser {
async loadManifest(runtimePaths: IRuntimePaths): Promise<INextManifest> {
async loadManifest(
config: IConfig,
runtimePaths: IRuntimePaths
): Promise<INextManifest> {
// Check whether static export mode
const staticExportMode = config?.output === 'export'

// Load build manifest
const buildManifest = await loadJSON<IBuildManifest>(
runtimePaths.BUILD_MANIFEST
runtimePaths.BUILD_MANIFEST,
!staticExportMode // Only throw error if output is not set to static export
)!

// Throw error if no build manifest exist
if (!buildManifest) {
if (!staticExportMode && !buildManifest) {
throw new Error(
'Unable to find build manifest, make sure to build your next project before running next-sitemap command'
)
Expand All @@ -35,7 +43,7 @@ export class ManifestParser {
)

return {
build: buildManifest,
build: buildManifest ?? ({} as any),
preRender: preRenderManifest,
routes: routesManifest,
}
Expand Down
40 changes: 40 additions & 0 deletions packages/next-sitemap/src/utils/__tests__/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,46 @@ describe('next-sitemap/defaults', () => {
})
})

test('withDefaultConfig: Static export', () => {
const myConfig = withDefaultConfig({
output: 'export', // Static output mode
generateRobotsTxt: true,
generateIndexSitemap: true,
sitemapSize: 50000,
exclude: ['1', '2'],
robotsTxtOptions: {
policies: [],
additionalSitemaps: [
'https://example.com/awesome-sitemap.xml',
'https://example.com/awesome-sitemap-2.xml',
],
},
})

expect(myConfig).toStrictEqual<Partial<IConfig>>({
output: 'export',
sourceDir: 'out',
outDir: 'out',
sitemapBaseFileName: 'sitemap',
generateIndexSitemap: true,
priority: 0.7,
changefreq: 'daily',
sitemapSize: 50000,
autoLastmod: true,
generateRobotsTxt: true,
exclude: ['1', '2'],
transform: defaultSitemapTransformer,
robotsTxtOptions: {
transformRobotsTxt: defaultRobotsTxtTransformer,
policies: [],
additionalSitemaps: [
'https://example.com/awesome-sitemap.xml',
'https://example.com/awesome-sitemap-2.xml',
],
},
})
})

test('withDefaultConfig: Default transformation', async () => {
const myConfig = withDefaultConfig({
trailingSlash: false,
Expand Down
29 changes: 28 additions & 1 deletion packages/next-sitemap/src/utils/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ export const defaultConfig: Partial<IConfig> = {
},
}

/**
* Set a preset for static export mode
* @param config
* @returns
*/
export const getStaticExportConfigPreset = (
config: Partial<IConfig>
): Partial<IConfig> => {
// Return empty preset for non static export
if (config?.output !== 'export') {
return {}
}

return {
sourceDir: 'out',
outDir: 'out',
}
}

/**
* Get default config
* @param config
* @returns
*/
export const withDefaultConfig = (config: Partial<IConfig>): IConfig => {
return overwriteMerge(defaultConfig, config)
// Add output.export config
const staticExportConfig = getStaticExportConfigPreset(config)

return overwriteMerge(defaultConfig, staticExportConfig, config)
}
6 changes: 5 additions & 1 deletion packages/next-sitemap/src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export const loadJSON = async <T>(
throwError = true
): Promise<T | undefined> => {
// Get path stat
const stat = await fs.stat(path)
const stat = await fs.stat(path).catch(() => {
return {
isFile: () => false, // Handle errors gracefully
}
})

// Return undefined or throw error
if (!stat.isFile()) {
Expand Down

0 comments on commit 706e1d3

Please sign in to comment.