NUXT - SLICE MACHINE - Get current document type from header-prismic

Greetings,

I'm building a language switcher, and I'd really need to know the current document type (i.e. home-page or page or custom type)

How can I achieve that? I tried everything by now.

Thanks!

Hey Marco, you can check the type of document by accessing the metadata of each document. Usually, you do this by using a Link Resolver. This is where you make links redirect to the correct URLs checking the lang and type attributes of the document.

Can you show me an example of how you're doing this in your project?

This thread has been closed due to inactivity. Flag to reopen.

Hi Pau, please forgive me, I hadn't seen the notification!

Thank you so much for your answer.

My problem is exactly this:

methods: {
    async asyncData() {
      try {
        let lang = { lang: this.$route.params.lang }
        const result = await this.$prismic.api.getByUID('page', this.$route.params.uid, lang)
        this.altLangs = result.alternate_languages;
      } catch (e) {
        console.log(e);
      }
    }
  },
  created() {
    this.asyncData();
  },

This is the code inside my _uid.vue file. It works fine, but my problem is, it only works for page type "page", because the call you need to make to get the language through the prismic API assumes you know the page type beforehand.

Let's assume I have 1 more different page type for whatever reason. It would work just fine being processed by my _uid.vue file because matter-of-factly is exactly the same as a normal "page" type, but I need it to be different to make it easier to query that specific document type in some parts of my software (I cannot use tags as they're already employed in the blog section).

Now, if in my _uid.vue file I could know what document type I'm currently handling, I could pass the "type" parameter dynamically to the prismic apy call, unifying the flow to my _uid.vue file.

The alternative to this of course is to create a folder name as the page type in my _lang folder

Screenshot 2021-09-24 at 11.07.58|195x161

as the screenshot I've uploaded. It seems quite dumb to me though, I really felt like there must be a better way I can't figure out somehow.

I hope this makes sense, I'm very thankful for your help.

P.S. My link resolver:

export default function (doc) {

  if (doc.isBroken) {
    return '/404';
  }

  if (doc.type === 'home-page') {
    return '/' + doc.lang
  }

  if (doc.type === 'page') {
    return '/' + doc.lang + '/' + doc.uid;
  }

  if (doc.type === 'blog-post') {
    return '/' + doc.lang + '/blog/' + doc.uid;
  }

  return '/404';
};

Marco

1 Like

Hello again @mb1, thank you for sending over all the details of your project. We will review this as soon as possible. :mantelpiece_clock:

Hello Marco!

You can bring all the existing typos in your repository by accessing $prismic.api.data.types.
Also, the query $prismic.api.query (" ") returns all documents if you pass it an empty string.

Now, in your case, you always need to call the Custom type in the query to also pass the uid and know exactly what document you are going to display.

Could you tell me what other Custom Types are you mentioning besides "Page"?

We have a multi-language sample project that has a language switcher. This could help you see how the logic for the switcher is done so you can implement it in your own project!

Hi Pau, thank you for your answer.

The types I have are the ones you see in my link resolver in my previous post.

I'll check the sample project you sent me in the meantime thank you!

Awesome, let me know if you need help with anything else afterward.

This thread has been closed due to inactivity. Flag to reopen.