question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

route.js: access helper in permalink

See original GitHub issue

Is 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 in allRequests 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 and data hooks but it can probably add complexity by hiding some functions, so we should avoid it I guess.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
nickreesecommented, Nov 11, 2020

@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 an i18nPermalink and use that in your permalink function permalink: ({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, the slug doesn’t have to be used in generating the permalink.)

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 the helpers object on bootstrap.

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

                    request.permalink = await this.routes[request.route].permalink({
                        request,
                        settings: { ...this.settings },
                    });

and change it to:

                    request.permalink = await this.routes[request.route].permalink({
                        request,
                        settings: { ...this.settings }, // this should be a readonly proxy... will fix this next release. 
                        helpers: createReadOnlyProxy_1.default(this.helpers, 'helpers', `${request.route} permalink function`),
                    });

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 the helpers.i18n.permalink may be a bit limiting.

0reactions
kiuKisascommented, Nov 17, 2020

Sure, I understand, what I mean is, once we’ll add helpers object to the permalink() 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found