rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.37k stars 1.86k forks source link

Inputs are not restored to original state after reload in firefox #3763

Open ismirsehregal opened 1 year ago

ismirsehregal commented 1 year ago

Shiny (latest CRAN version) inputs are restored after pressing the reload button in firefox 108.0.1 (64-bit) on Windows 10.

Expected behaviour:

If you reload a shiny application by clicking on your browser’s Reload button, it will start a new session on both the client and the server, losing the state on both sides.

This is the case in chrome.

Example coming from here:

library(shiny)

ui <- fluidPage(
  selectInput("select", "Values", LETTERS[1:5], selected='A'),
  radioButtons("radio", "Radio", LETTERS[1:5], selected='A'),
  checkboxGroupInput("check", "Checkbox", LETTERS[1:5], selected='A'),
  textOutput("serverout")
)

server <- function(input, output, session) {
  output$serverout <- renderText({
    print(reactiveValuesToList(input))
    paste(input$select, input$radio, paste(input$check, collapse = ", "), sep = " | ")
    })
}

shinyApp(ui = ui, server = server)

animation

cpsievert commented 1 year ago

I can replicate this on my Firefox+Mac, and upon a bit of digging, this is the result of a known "feature" specific to Firefox.

https://stackoverflow.com/questions/2699284/make-page-to-tell-browser-not-to-cache-preserve-input-values?noredirect=1&lq=1

Most of the advice surrounding this seems to be set autocomplete="off" on <input> (or <form>), which does indeed work for radioButtons() and checkboxGroupInput(), but not for selectInput() (I'm not sure why).

library(shiny)

autoCompleteOff <- function(x) {
  tagAppendAttributes(x, .cssSelector = "input", autocomplete = "off")
}

ui <- fluidPage(
  selectInput("select", "Values", LETTERS[1:5], selected='A') |>
    autoCompleteOff(),
  radioButtons("radio", "Radio", LETTERS[1:5], selected='A') |>
    autoCompleteOff(),
  checkboxGroupInput("check", "Checkbox", LETTERS[1:5], selected='A') |>
    autoCompleteOff()
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

As it turns out though, if you hold shift during refresh, the inputs will go back to their original state (regardless of whether autcomplete="off" is set).

I don't really know if this is possible, but maybe one could detect a refresh via beforeunload, and either force a shift+refresh, or present a dialog about it.

jcheng5 commented 1 year ago

Ooof, annoying. Does the server "see" the same values as the UI, at least?

ismirsehregal commented 1 year ago

@jcheng5 yes, the latest user input is also restored on the server side.

wch commented 1 year ago

This is the same as #2293. There are a couple of useful links to references in that issue.