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

Set cache control headers to avoid caching UI #3810

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions R/shinyui.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,32 @@ uiHttpHandler <- function(ui, uiPattern = "^/$") {
if (is.null(uiValue))
return(NULL)

if (inherits(uiValue, "httpResponse")) {
return(uiValue)
} else {
# Avoid caching the UI response to ensure that UI is always re-evaluated.
# This is necessary for getCurrentTheme() to be known, required for BS4+.
no_cache_headers <- list(
"Cache-Control" = "no-cache, no-store, must-revalidate",
"Pragma" = "no-cache",
"Expires" = "0"
)

if (!inherits(uiValue, "httpResponse")) {
html <- renderPage(uiValue, showcaseMode, testMode)
return(httpResponse(200, content=html))
uiValue <- httpResponse(200, content=html)
}

# 2023-04-23 jcheng5: Example app in PR comment
# https://github.com/rstudio/shiny/pull/3810#issuecomment-1513828996
# > I think we should always add the cache-busting headers. Without it,
# Connect and ShinyApps.io configurations will have problems in that the
# workerId could be a stale value.
if (
# The user has not set a `Cache-Control` policy within their UI func
!"Cache-Control" %in% names(uiValue$headers)
) {
# Disable caching for the UI's response
uiValue$headers <- utils::modifyList(uiValue$headers, no_cache_headers)
}

uiValue
}
}