Skip to content
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

Allows sitemap output on root of public folder #31130

Merged

Conversation

pedrolamas
Copy link
Contributor

Description

All this PR does is allow developers to set the output option on the gatsby-sitemap-plugin to "" (empty string) so that the files can be written to the root of the public folder.

Some (like me) might prefer that instead of creating a subfolder to store the files, plus this mitigates a separate issue where the index file always points to the root folder instead of the specified output folder.

Documentation

https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/

Related Issues

Fixes #31095

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Apr 30, 2021
@pedrolamas
Copy link
Contributor Author

FYI, I've tested this locally and works fine with an empty string but will fail with null value, hence why I didn't put that as allowed!

moonmeister
moonmeister previously approved these changes May 1, 2021
@KyleAMathews
Copy link
Contributor

Curious why an empty string vs /?

@pedrolamas
Copy link
Contributor Author

@KyleAMathews I tested with "/" and noticed that the header that was created had double slashes in it (so it showed "//sitemap-index.xml"), using a blank string fixed that!

@KyleAMathews
Copy link
Contributor

Ah ok, that's the bug that should be fixed then here —

href={withPrefix(withoutTrailingSlash(output) + `/sitemap-index.xml`)}

When joining paths, it's best to use path.join as that then takes care automatically of any possible doubled slashes e.g:

➜  gatsby-plugin-sitemap git: ✗ node
Welcome to Node.js v16.0.0.
Type ".help" for more information.
> const path = require(`path`)
undefined
> path.join(`/`, `/`)
'/'

@pedrolamas
Copy link
Contributor Author

I do wonder if this issue is showing in other bits, given this:

// TODO: Remove for v3 - Fix janky path/asset prefixing
const withPrefix = withAssetPrefix || fallbackWithPrefix

@moonmeister
Copy link
Contributor

I was going to summit the pr to convert to path.join but it probably is a better solution than an empty string. You're welcome to do it here.

@pedrolamas
Copy link
Contributor Author

AFAIK, path.join() should not be used for this, as we are talking about urls and not actual paths, and in Windows land, that can causes even bigger problems:

image

Having said that, path.posix.join() works, but once again, that is NOT the correct way to do this!

@pedrolamas
Copy link
Contributor Author

This looks more like it!

function urlJoin(...parts) {
return parts.reduce((r, next) => {
const segment = next == null ? `` : String(next).replace(/^\/+/, ``)
return segment ? `${r.replace(/\/$/, ``)}/${segment}` : r
}, ``)
}

But I do wonder if there's already some dependency that Gatsby is using to do this correctly?

@moonmeister
Copy link
Contributor

moonmeister commented May 1, 2021

Good call, stark overflow looks like it has some good suggestions. https://stackoverflow.com/questions/16301503/can-i-use-requirepath-join-to-safely-concatenate-urls

@pedrolamas
Copy link
Contributor Author

Given the comments from @KyleAMathews, I've changed the code to fix when output: "/" to ensure that the link is correctly created.

I've also added a small test to ensure this keeps the expected behavior.

@moonmeister
Copy link
Contributor

Thanks for the fix and especially for the tests.

@moonmeister moonmeister added topic: sitemap type: bug An issue or pull request relating to a bug in Gatsby status: awaiting reviewer response A pull request that is currently awaiting a reviewer's response and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels May 1, 2021
Copy link
Contributor

@moonmeister moonmeister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't run the code but looks good.

@KyleAMathews
Copy link
Contributor

Ah yeah, posix.join is what we want cause windows. We use that elsewhere in core https://github.com/gatsbyjs/gatsby/search?q=posix.join

Nice work @pedrolamas !

@KyleAMathews KyleAMathews merged commit e9504f2 into gatsbyjs:master May 1, 2021
@pedrolamas pedrolamas deleted the pedrolamas/sitemap-root-output branch May 1, 2021 17:11
@KyleAMathews
Copy link
Contributor

Published in gatsby-plugin-sitemap@4.1.0-next.1 (so you can install it with gatsby-plugin-sitemap@next)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting reviewer response A pull request that is currently awaiting a reviewer's response type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'gatsby-plugin-sitemap' doesn't generate a /sitemap.xml file
3 participants