Skip to content

Commit

Permalink
Merge pull request #663 from iamvishnusankar/fix/undefined-sitemap
Browse files Browse the repository at this point in the history
[Misc] Upgrades
  • Loading branch information
iamvishnusankar authored Jul 1, 2023
2 parents cd9992b + 53d82f3 commit ac697e8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
12 changes: 6 additions & 6 deletions packages/next-sitemap/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ export class CLI {
*/
async main() {
// Load config from `next-sitemap.config.js` along with runtimePaths info
const { config, runtimePaths } = await new ConfigParser().loadConfig()
const configParser = new ConfigParser()
const { config, runtimePaths } = await configParser.loadConfig()

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

// Generate url set
const urlSet = await new UrlSetBuilder(config, manifest).createUrlSet()
const urlSetBuilder = new UrlSetBuilder(config, manifest)
const urlSet = await urlSetBuilder.createUrlSet()

// Split sitemap into multiple files
const chunks = toChunks(urlSet, config.sitemapSize!)
Expand Down
15 changes: 15 additions & 0 deletions packages/next-sitemap/src/parsers/config-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ export class ConfigParser {
return withDefaultConfig(baseConfig.default)
}

/**
* Basic validation
* @param config
*/
async validateConfig(config: IConfig) {
if (!config?.siteUrl) {
throw new Error('Validation error: Missing siteUrl')
}

return config
}

/**
* Load full config
* @returns
Expand All @@ -90,6 +102,9 @@ export class ConfigParser {
// Update base config with runtime config
const config = await this.withRuntimeConfig(baseConfig, runtimePaths)

// Validate config
await this.validateConfig(config)

// Return full result
return {
config,
Expand Down
24 changes: 14 additions & 10 deletions packages/next-sitemap/src/parsers/manifest-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import { loadJSON } from '../utils/file.js'
import fg from 'fast-glob'

export class ManifestParser {
config: IConfig
runtimePaths: IRuntimePaths

constructor(config: IConfig, runtimePaths: IRuntimePaths) {
this.config = config
this.runtimePaths = runtimePaths
}
/**
* Return paths of html files if config.output = "export"
* @param exportFolder
Expand All @@ -36,34 +43,31 @@ export class ManifestParser {
)
}

async loadManifest(
config: IConfig,
runtimePaths: IRuntimePaths
): Promise<INextManifest> {
async loadManifest(): Promise<INextManifest> {
// Load build manifest
const buildManifest = await loadJSON<IBuildManifest>(
runtimePaths.BUILD_MANIFEST
this.runtimePaths.BUILD_MANIFEST
)!

// Throw error if no build manifest exist
if (config?.output !== 'export' && !buildManifest) {
if (this.config?.output !== 'export' && !buildManifest) {
throw Logger.noBuildManifest()
}

// Load pre-render manifest
const preRenderManifest = await loadJSON<IPreRenderManifest>(
runtimePaths.PRERENDER_MANIFEST
this.runtimePaths.PRERENDER_MANIFEST
)

// Load routes manifest
const routesManifest = await loadJSON<IRoutesManifest>(
runtimePaths.ROUTES_MANIFEST
this.runtimePaths.ROUTES_MANIFEST
)

// Get static export path when output is set as "export"
const staticExportPages = await this.getStaticExportPages(
config,
runtimePaths.STATIC_EXPORT_ROOT
this.config,
this.runtimePaths.STATIC_EXPORT_ROOT
)

return {
Expand Down

0 comments on commit ac697e8

Please sign in to comment.