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

Unexpected token in bootstrap-toc.js #2207

Closed
GregorDeCillia opened this issue Oct 5, 2022 · 3 comments · Fixed by #2209
Closed

Unexpected token in bootstrap-toc.js #2207

GregorDeCillia opened this issue Oct 5, 2022 · 3 comments · Fixed by #2209
Labels
bug an unexpected problem or unintended behavior

Comments

@GregorDeCillia
Copy link

Thank you very much for this exceptional R package

I am currently getting javascript errors in one of my pkgdown sites which seems to break several functionalities of pkgdown.js.
This includes (among others) the table of contents in the sidebar.

Uncaught SyntaxError: Unexpected token '<'          bootstrap-toc.min.js:1  
Uncaught ReferenceError: Toc is not defined         jquery-3.6.0.min.js:2
    at HTMLDocument.<anonymous> (pkgdown.js:7:5)
    at e (jquery-3.6.0.min.js:2:30038)
    at t (jquery-3.6.0.min.js:2:30340)

After looking closer in the chrome devtools, it seems bootstrap-toc.js is not properly loaded from https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.js which is the cdn location from the pkgdown template

<!-- bootstrap-toc -->
<script src="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.js"></script>

My browser is recieving HTML rather than JS from the cdn. Here is a screenshot where I prettified the contents of bootstrap-toc.min.js by hand for easier readability.

image

Any advice on how this might happen would be very much appreciated.

@GregorDeCillia
Copy link
Author

GregorDeCillia commented Oct 5, 2022

I was just able to reproduce this issue with a simple webpage, which baiscally just loads bootstrap-toc.

<head>
  <script src="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.js"></script>
</head>
<body></body>

Replacing the link with https://cdn.jsdelivr.net/gh/afeld/bootstrap-toc@1.0.1/dist/bootstrap-toc.min.js resolves the issue. This fix also works when the pkgdown sites are post processed.

url_pkgdown <- "https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.js"
url_custom  <- "https://cdn.jsdelivr.net/gh/afeld/bootstrap-toc@1.0.1/dist/bootstrap-toc.min.js"

for (file in list.files('docs/articles', pattern = "*.html", full.names = TRUE)) {
  readLines(file) %>% 
    sub(url_pkgdown, url_custom, ., fixed = TRUE) %>% 
    writeLines(file)
}

@GregorDeCillia GregorDeCillia changed the title bootstrap-toc.js is not available Unexpected token in bootstrap-toc.js Oct 5, 2022
@maelle maelle added the bug an unexpected problem or unintended behavior label Oct 6, 2022
@maelle
Copy link
Collaborator

maelle commented Oct 6, 2022

Thanks so much!

@GregorDeCillia
Copy link
Author

@maelle Thank you for this timely fix. I can confirm that my pgkgdown site no longer needs patching when it is built with pkgown@7fe9e80.

Note for future readers of this issue

This bug only affects pkgdown websites which use bootstrap 5. All sites using bootstrap 3 (the default) are not affected and still work with the current CRAN version of pkgdown (2.0.6). Already deployed pkgdown websites which use bootstrap 5 need to be rebuild with the dev-version of pkgdown. To check if your package is affected, make sure to clear the browser cache using Ctrl + F5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants