Query breaks after achieved a document that's linked

In “Case 1” I link to another case, named “Case 2”. If I unpublish Case 2. and go to the website, it breaks the page with:

SyntaxError: JSON.parse: unexpected end of data at line 1 column 133 of the JSON data

This has to do with the link being broken and breaking the query URL.

I can fix it by removing the link, but I have to search through all my documents and look where I might have linked to the unpublished document, which would not work with the number of documents we will be publishing.

Hi there,

Thank you for posting on Prismic community,

It seems to me if I understood well that the error that you are getting comes from the code you are using to call our API, and you might need to add a check in your code if the link content is not available.

If that is not the case, then can you please tell us what technology you are using as well a code snippet where you fetch the link content/or where you get the error.

Looking forward to your reply,
Fares

He Fares,
The syntax error came from the query url itself so it’s hard to pin point exactly where it goes wrong.
I use Nuxt.js and the code snippet where I query for data is:

async asyncData({ $prismic, params, error }) {
	try {
		const data = await $prismic.api.getByUID('case', params.uid, {
			fetchLinks: ['case.page_title', 'case.page_subtitle'],
		});

		const doc = data.data;

		return {
			hero: {
				// title required
				type: data.type,
				title: doc.page_title,
				subtitle: doc.page_subtitle,
				image: doc.page_image,
				description: doc.page_text,
			},
			slices: {
				body: doc.body,
			},
			upnext: doc.upnext,
			meta: {
				title: doc.meta_title ? doc.meta_title : $prismic.asText(doc.page_title),
				description: doc.meta_description ? doc.meta_description : undefined,
				image: doc.meta_image,
				fallback: doc,
			},
		};
	} catch (e) {
		// eslint-disable-next-line no-console
		console.error(e);
		error({ statusCode: 404, message: 'Page not found' });
	}
}

Hi there,

Thank you for sharing the code with us.

I guess you need to handle this in the template you are using to display the data, meaning to add a check if your query doesn’t return the requested data then you don’t display the link.

If that doesn’t solve the problem for you then can share with me your project code in a direct message in order to debug the issue for you.

Hi,

I had a look on the project you have share with me and I couldn’t reproduce the issue
Also I had a test on my local with this code and those results I’ve got

async asyncData({ $prismic, params, error }) {
    try{
      const document = (await $prismic.api.getByUID('page', params.uid, {
        fetchLinks: ['page2.title']
      })).data;
      console.log(document, "document")

      return {
        slices: document.page_content
      }
    } catch (e) {
      console.log(e)
      // Returns error page
      error({ statusCode: 404, message: 'Page not found' })
    }
  },

And when ‘page2’ is published then I get:

And when ‘page2’ is unpublished then I get:

Notice that I get type: “broken_type”

But I don’t get the error you are describing

Also I’ve noticed that in your code you are not accessing the data object and most importantly not accessing the ‘case2’ page in this

const data = await $prismic.api.getByUID('case', params.uid, {
    fetchLinks: ['case.page_title', 'case.page_subtitle'],
});

Where in fact you need to do something like this

const data = await $prismic.api.getByUID('case', params.uid, {
        fetchLinks: ['case2.page_title', 'case2.page_subtitle'],
});

So for us, this is a local issue in your project and might also need to update you dependencies

 "@nuxtjs/prismic": "^1.2.3",
    "cookies": "^0.8.0",
    "nuxt": "^2.13.3",
    "prismic-javascript": "^3.0.1"

to the latest versions

Please don’t hesitate to reach to us if you have any other inquiries,

Fares

Hello Fares,

Thank you for looking in to it for me, I have upgrades all my dependencies as you suggested and this has no effect on my issue.

looking more at the examples that you gave me I see a difference in what you trying to fetch and what I’m trying to fetch.

I have a setup a Content relationship to the same type (case which is a repeatable) because I want to link to a different Case, in your example you fetch a different type page2. Does this effect the output?

And as for if it is a local problem, I’m not sure how that could be because only that part of the page makes a query and that part breaks.

I will DM you the URL and leave the query broken for you to checkout what happens

1 Like

Hi,
I will forward this issue to our dev team, and they will reach back as soon as possible.

1 Like

Any updates on this issue? BTW I can’t change the tag to Nuxt.js which is what I used in my project.

Hi,

Our dev team is checking this issue, they need some extra info.

so to be able to reproduce the issue It would be useful if you can share with us the name of the prismic repository you are testing within a private message if confidential?

This issue has been closed due to inactivity.

This is something that's affecting our team as well. If we have a broken link in a document, we get a malformed JSON response:

{"page":1,"results_per_page":20,"results_size":1,"total_results_size":1,"total_pages":1,"next_page":null,"prev_page":null,"results":[

That's the entirety of the response body, which is invalid JSON because it's unclosed. I'm guessing this is also related to an issue I filed with the ruby-kit.

Can this be handled more gracefully in the API response? I think at the very least, there should be some warning in the Prismic UI when archiving a document that's linked on published documents because this small thing has the potential to break so much of our content.