Skip to content
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

Adddons: allow injecting an "always live" JavaScript file #11758

Merged
merged 8 commits into from
Nov 18, 2024

Conversation

humitos
Copy link
Member

@humitos humitos commented Nov 11, 2024

We talked about giving users a way to inject a JavaScript file they control using our Cloudflare Worker infrastructure to allow them manipulate frozen documentations.

This could be used in different ways to fix bugs or add features to a particular frozen set of docs or even to all the versions. The user can make usage of API data to filter by version or not
(e.g. if (versions.current == "v3.0") { .. do something ...} )

The script could live in Read the Docs itself using a relative URL, or outside it, using an absolute URL.

Example using Sphinx

  1. Tell Sphinx to include static folder
  2. Include a readthedocs.js file in your project
  3. Make AddonsConfig.userjsfile_src to be /en/full-feature/readthedocs.js from addons admin UX

The console will render the following:

Screenshot_2024-11-11_18-13-59

Related #11474
Related readthedocs/addons#431

We talked about giving users a way to inject a JavaScript file they control
using our Cloudflare Worker infrastructure to allow them manipulate frozen
documentations.

This could be used in different ways to fix bugs or add features to a particular
frozen set of docs or even to all the versions. The user can make usage of API
data to filter by version or not
(e.g. `if (versions.current == "v3.0") { .. do something ...} `)

The script could live in Read the Docs itself using a relative URL, or outside
it, using an absolute URL.
@humitos humitos changed the base branch from main to humitos/addons-embedded-http-header November 13, 2024 12:52
humitos added a commit to readthedocs/addons that referenced this pull request Nov 13, 2024
This is a new addon that allow users to inject a specific JavaScript file to all
the versions to all the documentation pages to allow them to fix issues in old
"frozen" (not able to re-build) versions.

Requires readthedocs/readthedocs.org#11758
@humitos
Copy link
Member Author

humitos commented Nov 13, 2024

I changed this approach to simplify it a little. Instead of using an HTTP header file, I implemented this as an addon. We are returning addons.userjsfile in the API response and is the addon responsibility to inject this file into the document.

I did the addon implementation at readthedocs/addons#431, and I like this implementation more since it follows the pattern we already have and it doesn't require complicated HTTP headers.

@humitos humitos marked this pull request as ready for review November 13, 2024 13:40
@humitos humitos requested a review from a team as a code owner November 13, 2024 13:40
@humitos humitos requested a review from stsewd November 13, 2024 13:40
@humitos humitos changed the base branch from humitos/addons-embedded-http-header to main November 13, 2024 14:09
@humitos humitos changed the base branch from main to humitos/addons-embedded-http-header November 13, 2024 14:10
@humitos humitos requested a review from ericholscher November 13, 2024 14:10
@agjohnson
Copy link
Contributor

The feature naming "User JS" could be more accurate I feel, these scripts aren't connected to users, especially on commercial where projects don't really have users -- only teams and members. This feels more like a custom script or extra script or some other phrase that signals usage more.

The script could live in Read the Docs itself using a relative URL, or outside it, using an absolute URL.

I think it's fine to start here.

We might eventually consider doing this automatically for the user though. If we made this field the source of the JS file instead of a URL, we could easily serve the file for the user (under /_/?). This would feel nicer than having the user maintain the JS in one of the project versions and having to rebuild the version (or pushing the file off to a third party host).

@humitos
Copy link
Member Author

humitos commented Nov 14, 2024

This feels more like a custom script or extra script or some other phrase that signals usage more.

I like custom script 👍🏼 . I will update the code with that name.

We might eventually consider doing this automatically for the user though. If we made this field the source of the JS file instead of a URL, we could easily serve the file for the user (under /_/?). This would feel nicer than having the user maintain the JS in one of the project versions and having to rebuild the version (or pushing the file off to a third party host).

Yeah, that makes sense to me. I didn't want to complicate things by uploading a file, but maybe a TextField is enough? However, it will immediately put a lot more pressure on the Read the Docs side for this. People will start asking for a nicer editor for that field, and also versioning, etc. I think this could escalate pretty quickly. I'm not opposed, but I think we can explore this more in a future iteration.

@agjohnson
Copy link
Contributor

agjohnson commented Nov 14, 2024

Yeah, I'd say we could explore a textfield, nothing fancy. I don't think we need to take on any additional work around an editor UI or versioning though, that seems like a lot for a minor feature. I'd only take on an editor interface if we had another use for it somewhere else in the UI. I'd sooner allow an API POST to update the addons config and let users manage their project's custom JS that way.

@humitos
Copy link
Member Author

humitos commented Nov 14, 2024

Yeah, I'd say we could explore a textfield, nothing fancy.

Do you mean in this PR or in the next iteration?

@humitos
Copy link
Member Author

humitos commented Nov 14, 2024

Maybe do we want to have both? customscript_src and customscript_content where you can define one or the other?

@agjohnson
Copy link
Contributor

Do you mean in this PR or in the next iteration?

Sorry, yeah later, I think it's fine to start with just a URL

Maybe do we want to have both?

Great point, supporting both doesn't seem hard. This sounds like a good plan for later

Copy link
Member

@ericholscher ericholscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really nice feature 💯. If we're gonna ship it, we should put some docs together, and probably a couple examples of when this is useful.



class Migration(migrations.Migration):
safe = Safe.after_deploy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice making them nullable 💯

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

customscript_enabled is not nullable, so this will still break when creating a new addons object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous migration makes it nullable during deploy, and then marks it not nullable after, which I thought would work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I didn't see there were two migrations. I guess Django will ask for a default value when running this migration (for the objects that were created while the field was null).

@humitos
Copy link
Member Author

humitos commented Nov 18, 2024

If we're gonna ship it, we should put some docs together, and probably a couple examples of when this is useful.

I'm gonna merge this PR and the addons one. However, this won't be exposed to users until we write the UI in ext-theme. I will create issues for these tasks.

@humitos humitos merged commit 079283b into humitos/addons-embedded-http-header Nov 18, 2024
7 of 8 checks passed
@humitos humitos deleted the humitos/user-js-file branch November 18, 2024 15:46
humitos added a commit to readthedocs/addons that referenced this pull request Nov 18, 2024
This is a new addon that allow users to inject a specific JavaScript
file to all the versions to all the documentation pages to allow them to
fix issues in old "frozen" (not able to re-build) versions.

Requires readthedocs/readthedocs.org#11758
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants