Gatsby sitemap generation fails on proxied subdirectory

TLDR; sitemap.xml not being generated on GatsbyJS site deployed to subdirectory

Attempting to set up two sites, a primary landing page site, and a gatsby blog.

This is what the redirect looks like on the primary site:

/blog/* https://mysite.netlify.com/blog/:splat 200!

If I navigate to https://mysite.com/blog/ the blog is being proxied correctly. The image assets are not being loaded correctly since they are in a subdirectory instead of public/. For now, I’ve simply added /blog/ in front of all of the image src URL’s as I was not able to get assetPrefix to work. This is a good working temporary solution.

For the blog, I’m using gatsby-ghost-starter, with the following netlify.toml file:

[build]
  command = "gatsby build --prefix-paths && cd public && mkdir blog | mv * blog && mv blog/_headers . && mv blog/_redirects ."
  publish = "public/"

[template]
  incoming-hooks = ["Ghost"]

I’m using pathPrefix to prepend /blog/ to all of the routes.

module.exports = {
    pathPrefix: `/blog`,

    siteMetadata: {
        siteUrl: config.siteUrl,
    },

The siteUrl in siteConfig.js is set to: “https://mysite.com/blog” without a trailing slash.

Everything seems to be working alright except for the sitemap and RSS generation. These are the log errors I’m seeing:

error { [Error: ENOENT: no such file or directory, open 'public/blog/sitemap.xml']
11:45:13 AM:   errno: -2,
11:45:13 AM:   code: 'ENOENT',
11:45:13 AM:   syscall: 'open',
11:45:13 AM:   path: 'public/blog/sitemap.xml' }
11:45:13 AM: error { [Error: ENOENT: no such file or directory, open 'public/blog/sitemap-pages.xml']
11:45:13 AM:   errno: -2,
11:45:13 AM:   code: 'ENOENT',
11:45:13 AM:   syscall: 'open',
11:45:13 AM:   path: 'public/blog/sitemap-pages.xml' }
11:45:13 AM: error { [Error: ENOENT: no such file or directory, open 'public/blog/sitemap-posts.xml']
11:45:13 AM:   errno: -2,
11:45:13 AM:   code: 'ENOENT',
11:45:13 AM:   syscall: 'open',
11:45:13 AM:   path: 'public/blog/sitemap-posts.xml' }
11:45:13 AM: error { [Error: ENOENT: no such file or directory, open 'public/blog/sitemap-tags.xml']
11:45:13 AM:   errno: -2,
11:45:13 AM:   code: 'ENOENT',
11:45:13 AM:   syscall: 'open',
11:45:13 AM:   path: 'public/blog/sitemap-tags.xml' }
11:45:13 AM: error { [Error: ENOENT: no such file or directory, open 'public/blog/sitemap-authors.xml']
11:45:13 AM:   errno: -2,
11:45:13 AM:   code: 'ENOENT',
11:45:13 AM:   syscall: 'open',
11:45:13 AM:   path: 'public/blog/sitemap-authors.xml' }

I’m stumped as to whats causing this.

Hi, @knowispow, and welcome to the Netlify community site. :slight_smile:

Have you considered solving this original issue with the /blog/ path prefix in a different way? This might resolve the issue with the paths being wrong and also resolve sitemap issue (which sounds like it is a side effect of the current workaround).

The alternative solution would be to reference the paths to the other assets as being relative to the current path. For example, currently the assets might look like this:

<script src="/example/pretend-javascript-file.js"></script>

It sounds like the current solution is to force adding a /blog prefix so that (purely hypothetical) reference become something like this:

<script src="/blog/example/pretend-javascript-file.js"></script>

Is that correct?

If so, would you be willing to make the references to the assets relative to the current path using a ./ reference like this instead?

<script src="./example/pretend-javascript-file.js"></script>

I’m not sure how that is done with Gatsby but a solution like that would be our usual recommended solution. This would remove the requirement for the /blog prefix and still reference the relative path correctly for the proxied site. Also, it will hopefully resolve the sitemap issue in the process.

​Please let us know if there are other questions and/or if this doesn’t resolve the issue.