Adding an Image Sitemap Endpoint to Your Site
PicPerf has the ability to automatically generate an image sitemap for your website based on images that have been requested within the last 60 days. An important piece to leveraging this feature is to add the sitemap in the Google Search Console. But in order to do that, the sitemap must be served from your domain.
Depending on your framework or hosting provider, it’s relatively simple to set this up so that the sitemap is served from your domain, while still being sourced from PicPerf. Here are a few examples for various tools & providers.
Web Frameworks
WordPress
The PicPerf WordPress plugin will automatically register a sitemap route and add it to the <head>
of your pages. That said, here’s how you might tackle it yourself:
In your WordPress theme’s functions.php
file (or within a custom plugin you have running on your site), add the following code.
This will expose a new endpoint to your site at https://your-domain.com/image-sitemap
returning the sitemap’s contents. Modify the code as you see fit.
Statamic or Laravel
The Statamic integration will automatically register a sitemap route and add it to your robots.txt
file. But if you’d like to wire it up yourself, here’s how that’d look:
Create a new route in your routes/web.php
file that fetches the contents, and returns the response with the appropriate Content-Type
header:
Again, modify as you see fit.
Astro
In order to add an image sitemap endpoint to an Astro site, you’ll either need to be using server or hybrid rendering. In your pages
directory, create an image-sitemap.ts
file and add the following contents:
After deploying, https://your-domain.com/image-sitemap
will be available to serve the required contents with the correct response headers.
Hosting Platforms
Vercel
You can use Vercel’s URL rewrite feature to create an endpoint in a project. In a vercel.json
file located in your root directory, add the following:
This rule will automatically route requests to PicPerf to fetch your sitemap data.
Cloudflare Pages
In your site’s root functions
directory, create a new file that will serve as the path for your new sitemap endpoint. I’ll use image-sitemap.ts
.
Again, after deploying, https://your-domain.com/image-sitemap
will be available to reference in your robots.txt
file and add to the Google Search Console.
Netlify
With Netlify, you can set up URL rewrites using its _redirects
file or netlify.toml
. Here’s what the latter might look like:
See Netlify’s documentation on the matter for more details.