golem
golem copied to clipboard
PWA compatibility: Allow addResourcePath to use / or . prefix to serve content at the app root
As discussed offline:
In a {golem} app, the app_ui.R contains some piece of code to make JS and CSS files in the www folder available to the app:
golem_add_external_resources <- function(){
add_resource_path(
'www', app_sys('app/www')
)
tags$head(
favicon(),
bundle_resources(
path = app_sys('app/www'),
app_title = 'golemReprex'
),
tags$link(rel="manifest", href="manifest.webmanifest")
#tags$script(src = "www/hello.js")
# Add here other external resources
# for example, you can add shinyalert::useShinyalert()
)
}
JS and CSS are recursively included in an htmlDependency by golem::bundle_resources such that we don't have to worry about it.
The addResourcePath is required because we run the app from a different folder of www, thereby making it visible from the server. This has however implications for any other files than JS and CSS (included in the dependency) such as any Progressive web app file like a manifest.webmanifest which has to be linked as /www/manifest.webmanifest. In a vanilla shiny apps, we don't need to prefix by www.
Here is a repo which presents a workaround for a progressive web app, I moved the app.R from ./R to inst/app (same as folder www) and inactivated the add_resource_path and bundle_resources (I don't want the service worker to go in an HTML dependency as it may cause registration issues):
https://github.com/DivadNojnarg/shinyMobileTest
Maybe we should have a look at addResourcePath and play a bit with {httpuv}.
The corresponding PWA is deployed here: https://dgranjon.shinyapps.io/shinyMobileGolemTest/ and below a little screenshot to show that the service worker registration and the manifest are successfully handled.

Debugging PWA is a hell so happy to help you ...
Everything in www is made available at /
Shiny provides two ways of serving static files (i.e., resources):
Static files under the www/ directory are automatically made available under a request path that begins with /.
I wonder if we can mimic that 🤔
Just curious - has there been any progress on this? Is it possible to use the {shiny.pwa} package within a golem shiny app? I can't seem to get it to work following the shiny.pwa instructions, assuming because of the issues noted above. Thanks!
@rbcavanaugh I've been trying to solve that, I think @DivadNojnarg has a "hack" around this that he might be able to share.
I seem to remember that the idea is to use golem::add_ui_server_files() then runApp() from inst/app
Hi @rbcavanaugh. There are solutions indeed like in this repository: https://github.com/RinteRface/unleash-shiny-2021/blob/6d08701edea36106be404f81216630afc3727527/R/app_ui.R. The app is powered by golem. Another example here with {shiny.pwa}: https://github.com/DivadNojnarg/golemPWA/blob/master/R/app_ui.R
Thanks @DivadNojnarg!
I'm not sure I can find where the PWA is initiated in the first repository. The latter looks promising, but I'm not exactly clear on how to deploy the app - is shinyapps.io a possibility? Or does it require shiny server. The error I get trying to deploy a cloned golemPWA with shinyapps.io is below, which makes me think that deploying from the inst/app folder will be a problem for shinyapps.io.
2021-12-21T18:20:40.560940+00:00 shinyapps[5359303]: Starting R with process ID: '43'
2021-12-21T18:20:40.561502+00:00 shinyapps[5359303]: Shiny application starting ...
2021-12-21T18:20:41.026824+00:00 shinyapps[5359303]: Error in value[[3L]](cond) :
2021-12-21T18:20:41.026840+00:00 shinyapps[5359303]: Could not find a root 'DESCRIPTION' file that starts with '^Package' in '/srv/connect/apps/test'. Are you in your project directory, and does your project have a 'DESCRIPTION' file?
2021-12-21T18:20:41.026841+00:00 shinyapps[5359303]: Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
2021-12-21T18:20:41.026864+00:00 shinyapps[5359303]: Shiny application exiting ...
2021-12-21T18:20:41.026859+00:00 shinyapps[5359303]: Execution halted
Running the app locally gives, so assume the output should be /www?
No output folder provided, dependencies will be attached but no service worker file will be created.If you need a service worker file, provide the path used my your application to serve static files (usually www) using the output argument or use shiny.pwa::createServiceWorker(output_folder) to create one based on the package default template.
Hey @rbcavanaugh
yes the issue is that PWA requires a manifest file to be available on / and golem serves its dependencies on /www 😞
I think right now David's solution might not be compatible with shinyapps io because it doesn't really allow to correctly deploy packages apps.
I'll try to work on this again soon and let you know
All my examples are hosted on shinyapps.io. I'll check the bundle I deployed and let you know.
the www path isn't recognised in my project ...
here is the error:
Warning (test-golem-recommended.R:2:3): app ui
cannot open file 'www/style.css': No such file or directory
Backtrace:
1. bftb:::app_ui()
at test-golem-recommended.R:2:2
11. htmltools::includeCSS("www/style.css")
12. base::readLines(path, warn = FALSE, encoding = "UTF-8")
13. base::file(con, "r")
Error (test-golem-recommended.R:2:3): app ui
Error in `file(con, "r")`: cannot open the connection
Backtrace:
1. bftb:::app_ui()
at test-golem-recommended.R:2:2
11. htmltools::includeCSS("www/style.css")
12. base::readLines(path, warn = FALSE, encoding = "UTF-8")
13. base::file(con, "r")
Skip (test-golem-recommended.R:72:5): app launches
Reason: interactive() is not TRUE
@gabrielburcea www/ exists in the browser, not on the server side :)