Skip to content

Commit

Permalink
Update To Work with Push-State Routing (#1689)
Browse files Browse the repository at this point in the history
* 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
thostetler authored and romanchyla committed Feb 7, 2019
1 parent ef4c85d commit 7cab5e1
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 43 deletions.
125 changes: 125 additions & 0 deletions nginx.conf
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;
}
}
13 changes: 13 additions & 0 deletions server.sh
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
45 changes: 3 additions & 42 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8"/>
<base href="/">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<meta name="google-site-verification" content="XxQuw38lagr8-TgtCyhAMP_6ELS9dy35fgrqo9dxw3o" />
<title>NASA/ADS Search</title>
<meta name="description" content="A powerful, streamlined new Astrophysics Data System" />
<link rel="preload" href="./styles/css/styles.css" as="style">
<link rel="preload" href="//cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js" as="script">
<link rel="preload" href="./shim.js" as="script">
<link rel="stylesheet" href="./styles/css/styles.css" />
<link rel="icon" type="image/png" sizes="32x32" href="./styles/img/favicon.ico"/>
<link rel="search"
Expand Down Expand Up @@ -129,51 +131,10 @@
}
}, 10000);
})();

</script>

<!-- start the discovery application -->
<script src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js" async></script>
<script>
/*
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);
}
})();
</script>
<script src="./shim.js"></script>
</body>
</html>
7 changes: 6 additions & 1 deletion src/js/mixins/discovery_bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,13 @@ function (
navigator.start(app);
navigator.router = app.router; // this feels hackish

var noPushState = location.search.indexOf('pushstate=false') > -1;

// Trigger the initial route and enable HTML5 History API support
Backbone.history.start(conf ? conf.routerConf : {});
var newConf = _.defaults({
pushState: noPushState ? false : undefined
}, conf && conf.routerConf);
Backbone.history.start(newConf);

$(document).on('scroll', function () {
if ($('#landing-page-layout').length > 0) {
Expand Down
92 changes: 92 additions & 0 deletions src/shim.js
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);
})();

0 comments on commit 7cab5e1

Please sign in to comment.