Generate a static Next.js site that can deactivate (almost) all JavaScript
Generate a static Next.js site that deactivates (almost) all JavaScript when the query parameter no_script
is set.
Based on Kitty’s original article: Error recovery with Next
Sometimes it can be useful to exclude JavaScript from a page completely, for example, if the scripts result in errors for a user for some reason. However, it is difficult to do this dynamically with a static site, unless there is a server-side solution that can render different HTML.
Like Kitty’s original solution, this approach is also based on some client-side JavaScript that checks if the no_script
query parameter is set. If it is not set, the Next.js scripts are injected into the page dynamically. These steps make it all work:
The solution still depends on JavaScript, obviously.
It’s necessary to use appendChild
to add the tags. When simply appending the tag-strings to body.innerHTML
, the scripts won’t execute.
As a result, some non-trivial logic based on DOMParser
and createElement
is necessary.
Unless NextHead is customized somehow, there will still be link tags in the page’s head that reference and pre-load the scripts.