How to get query parameters with helpers?


#1

I am using Ghost 2.4.0. And I was wondering is there anyway to output the query url in generated HTML?

Say, user opens http://localhost:2368/welcome?query=something and I want to get the exact same request URL and output it back to the user. In this case, I don’t need any kind of cache.

To be more specific, we want our Ghost blog to accommodate WeChat’s (the biggest social network in China and also a pain in the ass platform for developers) authentication method, which requires current URL as part of the hash string.

There’s a GitHub issue which I found describing the same need: https://github.com/TryGhost/Ghost/issues/5957

[update on 18 Dec 2018]: For internet’s sake, I ended up using Ghost in development mode, which I assumed doesn’t cache anything. And then I added one line (res.locals.wechatUrl = req.originalUrl) in themes/middleware.js and went on to write my own helper.


#2

As I explained in the issue you linked to, Ghost’s themes are static not dynamic.

To give a bit more detail - Ghost’s template rendering engine, which uses the helpers, all operates on the server side, templates are cached, and the HTML should be treated the same as if it was generated by a static site generator and assumed that it will be cached by browsers and any caches between your users and your site.

The query parameters will change per-request, and therefore these are dynamic. There is no way to get query parameters from a theme helper because the renderer doesn’t necessarily run on every request, so you’d end up with the behaviour of the first request being cached.

It’s not a case of not needing a cache, this is fundamentally how Ghost works.

If you want to get the current URL, it should be done with JavaScript that is dynamically loaded on the page.


#3

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.