@@ -23,6 +23,7 @@ registerClient <- function(client) {
23
23
24
24
25
25
.globals $ resourcePaths <- list ()
26
+ .globals $ resources <- list ()
26
27
27
28
.globals $ showcaseDefault <- 0
28
29
@@ -69,8 +70,41 @@ addResourcePath <- function(prefix, directoryPath) {
69
70
getShinyOption(" server" )$ setStaticPath(.list = stats :: setNames(normalizedPath , prefix ))
70
71
}
71
72
72
- # .globals$resourcePaths persists across runs of applications.
73
+ # .globals$resourcePaths and .globals$resources persist across runs of applications.
73
74
.globals $ resourcePaths [[prefix ]] <- staticPath(normalizedPath )
75
+ # This is necessary because resourcePaths is only for serving assets out of C++;
76
+ # to support subapps, we also need assets to be served out of R, because those
77
+ # URLs are rewritten by R code (i.e. routeHandler) before they can be matched to
78
+ # a resource path.
79
+ .globals $ resources [[prefix ]] <- list (
80
+ directoryPath = normalizedPath ,
81
+ func = staticHandler(normalizedPath )
82
+ )
83
+ }
84
+
85
+ resourcePathHandler <- function (req ) {
86
+ if (! identical(req $ REQUEST_METHOD , ' GET' ))
87
+ return (NULL )
88
+
89
+ path <- req $ PATH_INFO
90
+
91
+ match <- regexpr(' ^/([^/]+)/' , path , perl = TRUE )
92
+ if (match == - 1 )
93
+ return (NULL )
94
+ len <- attr(match , ' capture.length' )
95
+ prefix <- substr(path , 2 , 2 + len - 1 )
96
+
97
+ resInfo <- .globals $ resources [[prefix ]]
98
+ if (is.null(resInfo ))
99
+ return (NULL )
100
+
101
+ suffix <- substr(path , 2 + len , nchar(path ))
102
+
103
+ subreq <- as.environment(as.list(req , all.names = TRUE ))
104
+ subreq $ PATH_INFO <- suffix
105
+ subreq $ SCRIPT_NAME <- paste(subreq $ SCRIPT_NAME , substr(path , 1 , 2 + len ), sep = ' ' )
106
+
107
+ return (resInfo $ func(subreq ))
74
108
}
75
109
76
110
# ' Define Server Functionality
@@ -167,6 +201,7 @@ createAppHandlers <- function(httpHandlers, serverFuncSource) {
167
201
http = joinHandlers(c(
168
202
sessionHandler ,
169
203
httpHandlers ,
204
+ resourcePathHandler ,
170
205
reactLogHandler
171
206
)),
172
207
ws = function (ws ) {
0 commit comments