-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic Sitemap Copy Plugin (#1232) #1245
Draft
jstockdi
wants to merge
2
commits into
ProjectEvergreen:master
Choose a base branch
from
jstockdi:1232-dynamic-sitemap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# @greenwood/plugin-dynamic-sitemap | ||
|
||
## Overview | ||
Spiders love to spider. To show our love to all the spiders out there, this plugin reads | ||
the graph and renders a sitemap.xml. Currently, it handles up to 10000 content entries, warning | ||
after 9000 content entries. | ||
|
||
## Usage | ||
Add this plugin to your _greenwood.config.js_ and spread the `export`. | ||
|
||
```javascript | ||
import { greenwoodPluginDynamicExport } from '@greenwood/plugin-dynamic-sitemap'; | ||
|
||
export default { | ||
... | ||
|
||
plugins: [ | ||
greenwoodPluginDynamicExport({ | ||
"baseUrl": "https://example.com" | ||
}) | ||
] | ||
} | ||
``` | ||
|
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,30 @@ | ||
import fs from 'fs/promises'; | ||
|
||
const greenwoodPluginDynamicExport = (options = {}) => [{ | ||
type: 'copy', | ||
name: 'plugin-dynamic-sitemap', | ||
provider: async (compilation) => { | ||
|
||
const { baseUrl } = options; | ||
const { outputDir } = compilation.context; | ||
|
||
let sitemapXML = '<?xml version="1.0" encoding="UTF-8"?>\n'; | ||
sitemapXML += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'; | ||
|
||
compilation.graph.forEach(page => { | ||
sitemapXML += `<url><loc>${baseUrl}${page.outputPath}</loc></url>\n`; | ||
}); | ||
|
||
sitemapXML += '</urlset>'; | ||
|
||
const sitemapUrl = new URL('./sitemap.xml', outputDir); | ||
await fs.writeFile(sitemapUrl, sitemapXML); | ||
|
||
return { | ||
from: sitemapUrl, | ||
to: new URL('./sitemap.xml', outputDir) | ||
}; | ||
} | ||
}]; | ||
|
||
export { greenwoodPluginDynamicExport }; |
74 changes: 74 additions & 0 deletions
74
packages/plugin-dynamic-sitemap/test/copy.default.dynamic-sitemap.spec.js
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,74 @@ | ||
/* | ||
* Use Case | ||
* Run Greenwood build command with no config and this plugin. | ||
* | ||
* User Result | ||
* Should generate a bare bones Greenwood build with correctly built sitemap.xml | ||
* | ||
* User Command | ||
* greenwood build | ||
* | ||
* User Config | ||
* import { greenwoodPluginDynamicExport } from '@greenwood/plugin-dynamic-sitemap'; | ||
* | ||
* { | ||
* plugins: [{ | ||
* greenwoodPluginDynamicExport({ | ||
* "baseUrl": "https://example.com" | ||
* }) | ||
* }] | ||
* | ||
* } | ||
* | ||
* User Workspace | ||
* src/ | ||
* templates/ | ||
* artist.html | ||
* pages/ | ||
* index.md | ||
* about.md | ||
*/ | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { runSmokeTest } from '../../../test/smoke-test.js'; | ||
import { getSetupFiles, getOutputTeardownFiles } from '../../../test/utils.js'; | ||
import { Runner } from 'gallinago'; | ||
import { fileURLToPath, URL } from 'url'; | ||
|
||
describe('Build Greenwood With Dynamic Sitemap Plugin: ', function() { | ||
const LABEL = 'Using Dynamic Sitemap feature'; | ||
const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js'); | ||
const outputPath = fileURLToPath(new URL('.', import.meta.url)); | ||
let runner; | ||
|
||
before(function() { | ||
this.context = { | ||
publicDir: path.join(outputPath, 'public') | ||
}; | ||
runner = new Runner(); | ||
}); | ||
|
||
describe(LABEL, function() { | ||
|
||
before(function() { | ||
runner.setup(outputPath, getSetupFiles(outputPath)); | ||
runner.runCommand(cliPath, 'build'); | ||
}); | ||
|
||
runSmokeTest(['public', 'index'], LABEL); | ||
|
||
describe('Sitemap.xml should exist and be well formed', function() { | ||
|
||
it('should have one sitemaps file in the output directory', function() { | ||
const sitemapXML = fs.readFileSync(path.join(this.context.publicDir, './sitemap.xml')); | ||
console.log(sitemapXML); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
after(function() { | ||
runner.teardown(getOutputTeardownFiles(outputPath)); | ||
}); | ||
}); |
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,10 @@ | ||
import { greenwoodPluginDynamicExport } from '../src/index.js'; | ||
|
||
console.log(greenwoodPluginDynamicExport); | ||
export default { | ||
plugins: [ | ||
...greenwoodPluginDynamicExport({ | ||
'baseUrl': 'https://example.com' | ||
}) | ||
] | ||
}; |
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,4 @@ | ||
{ | ||
"version": "1.0.0", | ||
"type": "module" | ||
} |
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,3 @@ | ||
# About Us | ||
|
||
Lorem ipsum. |
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,3 @@ | ||
## Home Page | ||
|
||
Welcome! |
8 changes: 8 additions & 0 deletions
8
packages/plugin-dynamic-sitemap/test/src/templates/artist.html
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 @@ | ||
<html> | ||
|
||
<body> | ||
<h1>Welcome to the artist page.</h1> | ||
<content-outlet></content-outlet> | ||
</body> | ||
|
||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting approach and maybe it's just something I'm missing here but the main concern / limitation I see with this approach is that there is no way to programmatically build your own sitemap. To me, the advantage of the dynamic sitemap option was that could automatically build it up whatever way you need, even tapping into your own frontmatter / page metadata.
Referencing my back of the napkin example in #1232, which is basically just taking a page out of the Next.js playbook, I would expect users to be able to make a sitemap.xml.(js|ts) file in the root of their workspace (e.g. src/sitemap.xml.js, which would just export a function in which we pass the compilation to them, and they can build it up themselves and set their own values for all supported properties, since there are things they might want
As far as how to do this through greenwood, I think the API Resource as a reference is probably going to get us halfway there at least for development, such that hitting
https://localhost:1984/sitemap.xml
returns something.For build time, we maybe add something to the build step like we do for adapters, where if we have a sitemap (perhaps the graph can track this on init?) we
import
the that function from the user's file and write out the file for them?