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

Support for meta tags in Dash for R #142

Merged
merged 9 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion R/dash.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' assets_url_path = '/assets',
#' assets_ignore = '',
#' serve_locally = TRUE,
#' meta_tags = NULL,
#' routes_pathname_prefix = '/',
#' requests_pathname_prefix = '/',
#' external_scripts = NULL,
Expand All @@ -34,6 +35,9 @@
#' cannot use this to prevent access to sensitive files. \cr
#' `serve_locally` \tab \tab Whether to serve HTML dependencies locally or
#' remotely (via URL).\cr
#' `meta_tags` \tab \tab List of lists. HTML `<meta>`tags to be added to the index page.
#' Each list element should have the attributes and values for one tag, eg:
#' `list(name = 'description', content = 'My App')`.\cr
#' `routes_pathname_prefix` \tab \tab a prefix applied to the backend routes.\cr
#' `requests_pathname_prefix` \tab \tab a prefix applied to request endpoints
#' made by Dash's front-end.\cr
Expand Down Expand Up @@ -158,6 +162,7 @@ Dash <- R6::R6Class(
assets_url_path = '/assets',
assets_ignore = '',
serve_locally = TRUE,
meta_tags = NULL,
routes_pathname_prefix = NULL,
requests_pathname_prefix = NULL,
external_scripts = NULL,
Expand All @@ -181,6 +186,7 @@ Dash <- R6::R6Class(
private$suppress_callback_exceptions <- suppress_callback_exceptions
private$app_root_path <- getAppPath()
private$app_launchtime <- as.integer(Sys.time())
private$meta_tags <- meta_tags

# config options
self$config$routes_pathname_prefix <- resolve_prefix(routes_pathname_prefix, "DASH_ROUTES_PATHNAME_PREFIX")
Expand Down Expand Up @@ -1245,6 +1251,9 @@ Dash <- R6::R6Class(
},

index = function() {
# insert meta tags if present
meta_tags <- generate_meta_tags(private$meta_tags)

# generate tags for all assets
all_tags <- private$collect_resources()

Expand All @@ -1261,7 +1270,7 @@ Dash <- R6::R6Class(
'<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
%s
<title>%s</title>
%s
%s
Expand All @@ -1278,6 +1287,7 @@ Dash <- R6::R6Class(
</footer>
</body>
</html>',
meta_tags,
private$name,
favicon,
css_tags,
Expand Down
22 changes: 22 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,28 @@ generate_js_dist_html <- function(href,
}
}

generate_meta_tags <- function(metas) {
has_ie_compat <- any(vapply(metas, function(x)
x$name == "http-equiv" && x$content == "X-UA-Compatible",
logical(1)))
has_charset <- any(vapply(metas, function(x)
"charset" %in% names(x),
logical(1)))

tags <- vapply(metas,
function(tag) sprintf("<meta name=\"%s\" content=\"%s\">\n", tag$name, tag$content),
rpkyle marked this conversation as resolved.
Show resolved Hide resolved
character(1))

if (!has_ie_compat) {
tags <- c('<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n', tags)
}

if (!has_charset) {
tags <- c('<meta charset=\"UTF-8\">\n', tags)
}
return(tags)
}

# This function takes the list object containing asset paths
# for all stylesheets and scripts, as well as the URL path
# to search, then returns the absolute local path (when
Expand Down