-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update To Work with Push-State Routing (#1689)
* Update To Work with Push-State Routing Should use pushstate by default, and also convert all `#/` over when it sees them Remember to update Discovery.vars.js to set `pushState: true` * add base, fix bug * Add check for pushState * Adding nginx server for easier testing; run ./server.sh * move handler, add url flag * update to regex * remove template changes, update regex * update to allow for copy link
- Loading branch information
1 parent
ef4c85d
commit 7cab5e1
Showing
5 changed files
with
239 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
daemon off; | ||
|
||
worker_processes auto; | ||
|
||
worker_rlimit_nofile 10000; | ||
|
||
user nobody nogroup; | ||
|
||
pid /tmp/nginx.pid; | ||
|
||
events { | ||
worker_connections 2048; | ||
accept_mutex off; # "on" if nginx worker_processes > 1 | ||
use epoll; | ||
} | ||
|
||
|
||
http{ | ||
|
||
log_format main '{"remote_addr": "$remote_addr","X-Original-Forwarded-For": "$proxy_add_x_forwarded_for","X-Forwarded-For": "$remote_user","time_local": "$time_local","request": "$request","status": "$status","body_bytes_sent": "$body_bytes_sent","http_referer": "$http_referer","http_user_agent": "$http_user_agent","request_length": "$request_length","Authorization": "$http_Authorization","url-path": "$document_uri","query-string": "$query_string","X-Original-Uri": "$request_uri" ,"http_cookie": "$http_cookie","X-Amzn-Trace-Id": "$http_x_amzn_trace_id"}'; | ||
|
||
access_log /var/log/nginx/access.log main; | ||
error_log /var/log/nginx/error.log; | ||
|
||
server { | ||
listen 80 default_server; | ||
listen [::]:80 default_server; | ||
server_name _; | ||
|
||
location /ready { | ||
access_log off; | ||
return 200 "{\"ready\": true}"; | ||
} | ||
location /alive { | ||
access_log off; | ||
return 200 "{\"alive\": true}"; | ||
} | ||
|
||
location / { | ||
|
||
alias /app/production/; | ||
index index.html; | ||
|
||
include /etc/nginx/mime.types; | ||
|
||
expires 6M; | ||
|
||
|
||
# forbid browsers to cache the main index and the discovery.config....js | ||
location ^~ /discovery.config { | ||
expires 0; | ||
add_header Cache-Control "public"; | ||
} | ||
location ^~ /index { | ||
expires 0; | ||
add_header Cache-Control "public"; | ||
} | ||
|
||
# allow CORS requests for static files (the only thing we are serving | ||
# right now; this is necessary for embedded applicaitons loading js | ||
# from different urls) | ||
if ($request_method = 'OPTIONS') { | ||
add_header 'Access-Control-Allow-Origin' '*'; | ||
add_header 'Access-Control-Allow-Credentials' 'true'; | ||
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS'; | ||
add_header 'Access-Control-Allow-Headers' 'X-BB-Api-Client-Version,Keep-Alive,User-Agent,If-Modified-Since,Cache-Control,Content-Type'; | ||
# Tell client that this pre-flight info is valid for 20 days | ||
add_header 'Access-Control-Max-Age' 1728000; | ||
add_header 'Content-Type' 'text/plain charset=UTF-8'; | ||
add_header 'Content-Length' 0; | ||
return 204; | ||
} | ||
if ($request_method = 'GET') { | ||
add_header 'Access-Control-Allow-Origin' '*'; | ||
add_header 'Access-Control-Allow-Credentials' 'true'; | ||
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS'; | ||
add_header 'Access-Control-Allow-Headers' 'X-BB-Api-Client-Version,Keep-Alive,User-Agent,If-Modified-Since,Cache-Control,Content-Type'; | ||
} | ||
} | ||
|
||
|
||
location /search/ { | ||
alias /app/production/; | ||
index index.html; | ||
include /etc/nginx/mime.types; | ||
try_files $uri /index.html; | ||
} | ||
|
||
location /abs/ { | ||
alias /app/production/; | ||
index index.html; | ||
include /etc/nginx/mime.types; | ||
try_files $uri /index.html; | ||
} | ||
|
||
location /user/ { | ||
alias /app/production/; | ||
index index.html; | ||
include /etc/nginx/mime.types; | ||
try_files $uri /index.html; | ||
} | ||
|
||
location /index/ { | ||
alias /app/production/; | ||
index index.html; | ||
include /etc/nginx/mime.types; | ||
try_files $uri /index.html; | ||
} | ||
|
||
location /execute-query/ { | ||
alias /app/production/; | ||
index index.html; | ||
include /etc/nginx/mime.types; | ||
try_files $uri /index.html; | ||
} | ||
|
||
} | ||
|
||
server { | ||
listen 8080; | ||
server_name _; | ||
return 200; | ||
access_log off; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
# make sure the image is built | ||
if [[ `docker images | grep bbb-nginx` == "" ]]; then | ||
docker build -t bbb-nginx . | ||
fi | ||
|
||
home=`pwd` | ||
target=${1:-src} # workdir that will become webserver's root | ||
|
||
|
||
docker rm -f bbb-nginx 2>/dev/null | ||
docker run -p 8000:80 -v $home/$target/:/app/production -v $home/nginx.conf:/etc/nginx/nginx.conf bbb-nginx nginx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
(function () { | ||
/* | ||
Dynamically pick which configuration to use based on the url. | ||
Then attempt to load the resource, using require, upon failure we | ||
load a known resource (discovery.config.js) | ||
*/ | ||
var paths = { | ||
'': 'landing-page', | ||
'search': 'search-page', | ||
'abs': 'abstract-page' | ||
}; | ||
var load; | ||
try { | ||
var loc = window.location; | ||
var path = loc[loc.pathname === '/' ? 'hash' : 'pathname'].split('/')[0].replace('#', ''); | ||
load = function () { | ||
// attempt to get bundle config | ||
require([paths[path] + '.config'], function() { | ||
// do nothing | ||
}, function() { | ||
// on failure to load specific bundle; load generic one | ||
require(['discovery.config']); | ||
}); | ||
}; | ||
} catch (e) { | ||
load = function () { | ||
// on errors, just fallback to normal config | ||
require(['discovery.config']); | ||
}; | ||
} | ||
|
||
(function checkLoad() { | ||
if (window.requirejs) { | ||
return load(); | ||
} | ||
else { | ||
setTimeout(checkLoad, 10); | ||
} | ||
})(); | ||
|
||
var setGlobalLinkHandler = function () { | ||
|
||
var routes = [ | ||
'classic-form', | ||
'paper-form', | ||
'index', | ||
'search', | ||
'execute-query', | ||
'abs', | ||
'user', | ||
'orcid-instructions', | ||
'public-libraries' | ||
]; | ||
var regx = new RegExp('^#(\/?(' + routes.join('|') + ').*\/?)?$', 'i'); | ||
|
||
// apply a global link handler for push state | ||
require(['jquery'], function ($) { | ||
|
||
var $el = []; | ||
$(document).on('mousedown', 'a', function (ev) { | ||
if (!Backbone.history.options.pushState) return; | ||
$el = $(ev.currentTarget); | ||
var href = $el.attr('href'); | ||
if (regx.test(href)) { | ||
var url = href.replace(/^\/?#\/?/, '/'); | ||
$el.attr('href', url); | ||
return false; | ||
} | ||
$el = []; | ||
}); | ||
|
||
$(document).on('click', 'a', function () { | ||
if ($el.length && window.bbb) { | ||
var href = $el.attr('href'); | ||
|
||
// clear it so we don't have one lingering around | ||
$el = []; | ||
try { | ||
var nav = bbb.getBeeHive().getService('Navigator'); | ||
nav.router.navigate(href, { trigger: true, replace: true }); | ||
return false; | ||
} catch (e) { | ||
console.error(e.message); | ||
} | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
setTimeout(setGlobalLinkHandler, 1000); | ||
})(); | ||
|