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

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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

rylax
Copy link

@rylax rylax commented Aug 17, 2021

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.

@atinux atinux requested a review from NicoPennec August 17, 2021 23:53
@MeHattabi
Copy link

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

Copy link
Member

atinux commented Aug 24, 2021

Do you mind taking a look at it @NicoPennec ?

@NicoPennec
Copy link
Member

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.

@TitanFighter
Copy link

TitanFighter commented Aug 25, 2021

@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.

@rylax
Copy link
Author

rylax commented Aug 26, 2021

@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 😬

@TitanFighter
Copy link

TitanFighter commented Sep 7, 2021

@NicoPennec could you check this PR please?

@MeHattabi
Copy link

Is this going to get picked up someday?

@avral
Copy link

avral commented Oct 15, 2022

we need this feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants