-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: More reliable Edit Button for static sites #94
Comments
Thanks for writing this up @angeloashmore ! For strategy 1, is there a decent way we could add it to our kits? Is it possible that by using a in React, we can use Context to inform a component that a certain document has been requested? Then automatically build out that information on the page? I don't think this would work with either Gatsby or Next's data fetching methods but wanted to raise the idea. Or a function that takes in your Prismic response and pulls out all the IDs included? We might get some false positives if we're not careful, but any way we can make it as simple and foolproof to implement would help with Strategy 1. Overall I think Strategy 1 is the best, least prone to mistakes, and if we're already teaching them how to do things like installing previews in Next, this doesn't seem like a ton more work. Still, if we can automate it in any way, that'd be a win. Strategy 2 as a fallback makes sense as a second-best option. |
The method that I experimented with Gatsby was simply to resolve all links between documents and url at build time on the kit level. Since you programmatically build your pages along with their url, you can build the edit button pretty accurately |
Strategy 1 seems the most robust, for me the number of issues that we might not even see yet with strategy 2 makes it much less viable (Also scary for me as a support agent). Can I ask why you decided against the route resolver strategy, other than the time to wait for updating the route resolver functionality to match all the use cases of the linkresolver? |
Strategy 1 is more robust but it's also what we avoided and actually got rid of from the first version. Having to do it manually but if it's an extra step not mandatory to get better accuracy it's probably for the best. But still at build time it's probably the better timing for a static app |
There's un-merged PR that attempted strategy 2 For strategy one,
|
My vision of the issue when we discussed the above with Angelo: First, the goal is to support the main document prediction for our main frameworks (Nuxt, Next, Gatsby) without/with very minimal user action required (at most, upgrading to the latest version of a kit). Getting non-main document predictions to work is purely a bonus and not something we particularly aimed for when considering that reduced scope. Second, I don't want our SDKs to be (heavily) impacted in order to propose more accurate predictions, two reasons for that:
I strongly believe that a long-term solution to the edit button issue involves fixing the route resolver and having the Prismic repository be aware of it (by defining it within it perhaps/uploading it from code/Slice Machine). Such long-term solution involves development to happen on the product side, which the product team doesn't have bandwidth for as of talking (afaik, cc @phillysnow), therefore the above solutions can also be seen as more realistic solutions that we can roll out within a short time frame without relying on the product team. To me, the two strategies propose an interesting synergy:
This could be framed in the documentation in that fashion:
Regarding strategy 1: If we want to allow multiple meta tags, then we need a convention for defining the main document (otherwise we could just have said "first in the array is the main document"), except if we want to pass all those documents to the existing sorter ( https://github.com/prismicio/prismic-toolbar/blob/master/src/iframe/prediction.js#L10-L25, which comes with limitations/accuracy impact). Random suggestions (no real preferences): Separate meta tag (clean but maybe not convenient) <meta name="prismic-main-document" content="U0V72AEAAE4netWJ" />
<meta name="prismic-documents" content="U0V72AEAAE4netWJ,YfmNTRIAAC8AiccC" /> Data attribute (quite dirty IMO) <meta name="prismic-documents" data-main="U0V72AEAAE4netWJ" content="U0V72AEAAE4netWJ,YfmNTRIAAC8AiccC" /> Prefix (I think it's the best compromise between clean & convenience) <meta name="prismic-documents" content="main:U0V72AEAAE4netWJ,YfmNTRIAAC8AiccC" /> JSON (while still supporting CSV format, too unconveninet I think) <meta name="prismic-documents" content='{"main": "U0V72AEAAE4netWJ", "others": ["YfmNTRIAAC8AiccC"] }' /> Strategy 1 also suffers from the access token limitation of strategy 2 as the toolbar would need to get summaries/titles of those documents which are currently supplied by the Regarding strategy 2 limitations:
I think strategy 2 should rely on the UID and the document H1 if any, we could perform a full text search using the H1 on the API when the UID is not found or ambiguous.
To deal with home pages, I think we could handle the
Let's not support that use case yeah.
Same, let's keep those as false positives. Regarding the shared access token limitation: Again, ideally, this could be bypassed by the product but just providing the data attributes of strategy 1 and the document URL and H1 from strategy 2 to the I'm not a fan of asking users to add their access token as a data-token attribute on the toolbar script. This could do it although. Or we could just not support private repositories (@Duaner do we have a ratio/idea of the percentage of private vs public repo?) |
Thanks for the comments everyone! @arnaudlewis Performing this computation at build-time is similar to the idea of providing a Route Resolver to Prismic. It allows a manifest of URLs mapped to their documents which could be saved to a public file or sent to Prismic. @lihbr and I discussed this idea, but for reasons stated in the above comment, it would require specific implementations for each framework. Someone using 11ty, for example, would not be able to take advantage of the improved Edit Button accuracy until there was an official integration. The manifest concept is still a great idea which, theoretically, can be achieved for all users through an improved Route Resolver integration. @lihbr I agree with everything said there. The two ideas proposed here are solutions we can implement quickly and scale to all sites. The long-term solution of improving and uploading a Route Resolver will provide a better experience overall. Strategy 1 (declaring documents with
I think a separate meta element is the simplest. It's a little more verbose than other ideas like <meta name="prismic-main-document" content="U0V72AEAAE4netWJ" />
<meta name="prismic-documents" content="U0V72AEAAE4netWJ,YfmNTRIAAC8AiccC" />
You're right; both solutions rely on access to It is not a good idea to exclude private repositories as well (I believe doing this would also break when using Releases). We would be recommending users make all their content public which may not be acceptable for some users. Since the Edit Button only appears for logged in users, could we have a special Toolbar-specific endpoint that is authenticated via cookies rather than Access Token? We would need to come up with a spec for these new endpoints. |
Overview
This RFC proposes a way to improve the Prismic Toolbar’s Edit Button by more accurately reporting documents used on a page in static sites.
What and/or who is affected by this RFC?
getServerSideProps
runs in a different context than the page’s React component.Static site generators and architectures that separate data fetching from presentation will be referred to as “static sites” for the rest of the RFC.
What problem does this RFC solve?
The Edit Button appears as part of the Prismic Toolbar. It is designed to give editors and developers a quick way to edit content that appears on a page. It does this by listing Prismic documents that the Prismic Toolbar thinks is on the page with a direct link to their pages in the Writing Room.
The toolbar often shows an inaccurate list of documents on static sites. Usually, this list will only show a subset of the documents used. In some cases, the toolbar does not detect any documents on the page. In that case, the edit button is not displayed at all.
This RFC proposes an improved algorithm that should result in a more accurate document list for most static sites.
Note: This proposal does not attempt to cover 100% of the possible cases. Doing so would require extensive engineering which is not possible at this time. That will happen some time in the future.
What prompted this RFC?
The growing adoption of static site frameworks, especially among Prismic users, has magnified the Edit Button’s limitations. Since more users are encountering this issue, the issue should be fixed.
Are there any related discussions from elsewhere?
I am not aware of any discussions. That’s not to say there have not been any!
Proposal
The problem can be addressed using two strategies:
Both strategies should be added to the toolbar’s existing algorithm. The new strategies take priority over the existing algorithm in the order they are listed above.
Due to Strategy #2, the Edit Button should produce more positive results without extra effort from a developer.
If a developer wants 100% accuracy, they can manually declare which documents appear on the page with Strategy #1.
Details for each are provided below.
Strategy 1: Allow users to tell the Prismic Toolbar which documents appear on the page
In many cases, a developer programmatically knows which documents are used on a page. Content from the Prismic API includes a document ID. These document IDs can be included in a page’s HTML which can be read by the Prismic Toolbar.
This can handle cases where secondary content is queried in areas like a page’s layout. This could include a Navigation document in the header, or a Settings document to control metadata. The IDs of these documents can easily be fetched.
Developers can declare which documents appear on the page through a special Prismic-specific
<meta>
element namedprismic-documents
in the page’s<head>
.Multiple instances of this
<meta>
element can be present to ease the declaration process. For example, a developer can add a<meta>
element on a global level, such as in a Layout component, and further down the tree in a specific component, such as in a Blog Post component.<head>
manager APIs built in to frameworks can facilitate this process. Next.js, for example, provides a<Head>
component to control elements in the<head>
.The toolbar will read these document IDs to produce the list displayed in the Edit Button’s interface.
Communication Logic
The Prismic Toolbar iframe cannot read these elements directly due to security limitations. Instead, it needs to communicate with the page via a messaging channel and an on-page script. The toolbar is added to a site using a
<script>
element which allows us to embed the communication logic outside the iframe.Code within the toolbar’s bootstrap script should be kept minimal. It should only listen for a request from the toolbar’s iframe to fetch a list of document IDs. It should respond with the list using a minimal amount of code. All other logic, such as parsing the document ID list, should be done within the iframe.
To ensure the toolbar’s list is always up to date and accounts for any lazily added document IDs, some form of pushing updates to the toolbar should be included. This could be implemented with a MutationObserver or polling.
Limitations
Strategy 2: Detect at least the page’s primary page by detecting a document’s UID in the page’s URL
In many cases, a page’s URL contains a document’s unique ID (UID). Of those cases, the UID is usually the right-most segment of a URL path.
In the above cases,
about-us
andjane-doe
may be document UIDs. The toolbar can read the URL and determine if those segments have a matching document.UIDs are only unique within a Custom Type, so the UID must be queried against every Custom Type in the site’s Prismic repository. The toolbar can query the Prismic repository’s
/api/v2
endpoint to retrieve a list of its Custom Types. Note that this query may require an access token, which the toolbar currently does not require.Limitations
/api/v2
request may require an access token. Users would need to provide this somehow in the browser, potentially negating the purpose of the access token./
which does not have any segments to query against./blog/hello-world-YfmNTRIAAC8AiccC
./about
may match with a document even if that document is not used on the page.Rejected strategies
The two above strategies were selected for their relative ease of implementation and possible timely deployment. The following ideas were rejected within this scope:
These ideas may be revisited during a more extensive Prismic Toolbar refresh.
How to provide feedback
Anyone can provide feedback on anything addressed in this RFC. To provide feedback, please comment on this issue.
While everything is open for feedback, the following questions are most important:
Thanks! 🙂
The text was updated successfully, but these errors were encountered: