sitemap-module icon indicating copy to clipboard operation
sitemap-module copied to clipboard

feat: Support option to access req.headers.host in dynamic routes function

Open rylax opened this issue 4 years ago • 8 comments

This PR allows you to access req.headers.host when dynamically generating routes as requested in #217 #216 #215 #11

✨ Helpful for multi-tenant projects where the subdomain is needed to identify a user [user].maindomain.com

In order to use it the dynamicHost option must be enabled. Then the host parameter is available to be used in the routes function.

See example:

sitemap: {
    dynamicHost: true,
    routes: async (host) => {
      const subdomain = host.split('.')[0]
      const res = axios.get('https://myapi.com/getAllPages?user=' + subdomain)
      return res.data
     }
}

––––––––––––-

This is my first PR ever on Github and the solution is quite hacky – so please take it with a grain of salt and see it more as an inspiration on how this functionality could be achieved.

rylax avatar Aug 17 '21 18:08 rylax

I'd like this aswell, I've used your "hacky" solution, works perfect.

MeHattabi avatar Aug 23 '21 07:08 MeHattabi

Do you mind taking a look at it @NicoPennec ?

atinux avatar Aug 24 '21 08:08 atinux

Hi @rylax & @Atinux, I just came back from holidays.

Using the hostname as the cache ID sounds like a smart idea. I will be looking in depth this week. Thanks @rylax for this proposal.

NicoPennec avatar Aug 24 '21 12:08 NicoPennec

@rylax

in your commit it is necessary to restore the 25th line }, in order to close async load(key, callback) { in lib/cache.js. The rest is ok and everything works.

TitanFighter avatar Aug 25 '21 14:08 TitanFighter

@rylax

in your commit it is necessary to restore the 25th line }, in order to close async load(key, callback) { in lib/cache.js. The rest is ok and everything works.

Thanks for pointing it out. Corrected it now. One can see that this is my first PR as the commit doesn't seem to go through the checks either 😬

rylax avatar Aug 26 '21 13:08 rylax

@NicoPennec could you check this PR please?

TitanFighter avatar Sep 07 '21 17:09 TitanFighter

Is this going to get picked up someday?

MeHattabi avatar Nov 03 '21 19:11 MeHattabi

we need this feature

avral avatar Oct 15 '22 19:10 avral