route.js: access helper in permalink
See original GitHub issueIs your feature request related to a problem? Please describe.
I start working on a i18n plugin which , among other things, generate requests and permalink and save it to a dictionnary in plugin
for later use (e.g: /pikachu
become /fr/pikachu
and /en/pikachu
).
Since permalink
doesn’t have access to helper, it make it more difficult to add permalink to a dictionnary and to implement some features, for examples:
- “prefix_except_default” (add i18n.code except for the default locale)
- “permalink replacement” if we want to change the permalink for different locales, (e.g:
/en/bulbasaur
and/fr/bulbizarre
) - and probably others…
Describe the solution you’d like
I can add an helper to generate i18n requests in all
as the following:
all: async ({ helpers }) => helpers.i18n.requests([{ slug: 'pikachu' }])
So it can be great to be able to do the same for permalink
like this:
permalink: ({ request, helpers }) => helpers.i18n.permalink(request, `pokemon/${request.slug}/`),
Describe alternatives you’ve considered
- I consider rewriting the
slug
params inallRequests
hook but it doesn’t feel right at all and will add others issues for hreflang and probably other things. - we can also add
all
,permalink
anddata
hooks but it can probably add complexity by hiding some functions, so we should avoid it I guess.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Accessing URL helpers in routes.rb - ruby on rails
So I'm guessing one cannot use URL helpers in routes.rb. Is there a way to use URL helpers in the route file, and...
Read more >Helpers - Routify
$url resolves paths relative to the page/layout file in which it is used. This ensures consistent URLs that are unaffected by the current...
Read more >CMS with Ember nice URL slugs and link to helper - Routing
Hi, I'm creating a small CMS with a Drupal backend (https://www.contentacms.org/) for Ember. In the backend I have a field to define a...
Read more >Dynamic Routes - Next.js
Any route like /post/1 , /post/abc , etc. will be matched by pages/post/[pid].js . The matched path parameter will be sent as a...
Read more >ActionView::Helpers::UrlHelper - Rails API
... in any class that includes the URL helpers of a routes (routes.url_helpers). ... Prior to Rails 7, Rails shipped with a JavaScript...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kiuKisas
Interesting. Your way will work and I think the permalink function likely should have access to the helpers. (see below)
That said, you could also change the array returned by
helpers.i18n.requests
to include ani18nPermalink
and use that in yourpermalink
functionpermalink: ({request}) => request.i18nPermalink,
instead of calling the helpers.(NOTE: Elder.js does enforce a
slug
to be on each request to keep people from shooting themselves in the foot, theslug
doesn’t have to be used in generating thepermalink
.)Helpers to Permalink
If you still want to explore adding the helpers to the permalink here is how:
You’ll need to add the
i18n
helper to thehelpers
object onbootstrap
.Before we implement this in the core, can you test it locally and make sure it works well for you?
Look for this line in your local copy of Elder.js:
node_modules/@elderjs/elderjs/build/Elder.js
and change it to:
Other
Have you given thought to how users would use the
helpers.permalinks.pokemon({request})
helper to build links? It seems that the signature of thehelpers.i18n.permalink
may be a bit limiting.Sure, I understand, what I mean is, once we’ll add
helpers
object to thepermalink()
function, if we use this feature,helpers.permalink
will not work (and we have to add a new permalinks helper as you suggest) . It can be confusing for someone who is not award of that; so I propose to write something about it in the documentation once the PR is merge.