Skip to content
This repository was archived by the owner on Aug 9, 2018. It is now read-only.

Commit 4c519b5

Browse files
Allow the app to change the $location path while rendering.
This gets handled as a HTTP redirect. In the present implementation this is a bit wasteful because it will still completely render the page even though the body isn't actually returned.
1 parent 08e1fd8 commit 4c519b5

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

lib/main.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ function handleRequest(template, context, modules, request, response) {
6060
link($rootScope);
6161

6262
var onceRouteKnown = function (route) {
63+
64+
if (reqContext.getRedirectTo()) {
65+
// The path changed while we were rendering, so emit a redirect.
66+
// FIXME: Find some way to do this without actually doing the work to render
67+
// the route. Probably will need to do route resolution manually, then.
68+
response.writeHead(
69+
302,
70+
{
71+
Location: reqContext.getRedirectTo()
72+
}
73+
);
74+
response.end(reqContext.getRedirectTo());
75+
// still need to clean up the context once everything's settled.
76+
$httpBackend.notifyWhenNoOpenRequests(
77+
function () {
78+
context.dispose();
79+
}
80+
);
81+
return;
82+
}
83+
6384
// Stash any controller locals we pre-resolved so we can return them
6485
// to the client as a sort of cache so the browser can avoid additional round-trips.
6586
var preResolved = {};
@@ -73,7 +94,6 @@ function handleRequest(template, context, modules, request, response) {
7394

7495
$httpBackend.notifyWhenNoOpenRequests(
7596
function () {
76-
// once more for luck
7797
$rootScope.$digest();
7898

7999
response.writeHead(200);

lib/ngoverrides.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ function registerModule(context) {
88

99
module.factory(
1010
'serverRequestContext',
11-
function () {
11+
function ($rootScope) {
1212
var request;
1313
var requestUrlParts;
14+
var redirectTo;
1415

1516
var ifRequest = function (code) {
1617
return function () {
@@ -23,30 +24,33 @@ function registerModule(context) {
2324
};
2425
};
2526
var parsedUrl = function (code) {
26-
return function () {
27+
return function (set) {
2728
if (request) {
2829
if (! requestUrlParts) {
2930
requestUrlParts = url.parse(request.url, true);
3031
}
31-
return code(requestUrlParts);
32+
return code(requestUrlParts, set);
3233
}
3334
else {
3435
return undefined;
3536
}
3637
};
3738
};
3839

40+
var absUrl = parsedUrl(
41+
function (parts) {
42+
// TODO: Make this be https: when the
43+
// request is SSL?
44+
return 'http://' +
45+
request.headers.host +
46+
parts.pathname +
47+
(parts.search ? parts.search : '');
48+
}
49+
);
50+
3951
return {
4052
location: {
41-
absUrl: ifRequest(
42-
function () {
43-
// TODO: Make this be https: when the
44-
// request is SSL?
45-
return 'http://' +
46-
request.headers.host +
47-
request.url;
48-
}
49-
),
53+
absUrl: absUrl,
5054
hash: function () {
5155
// the server never sees the fragment
5256
return '';
@@ -57,7 +61,21 @@ function registerModule(context) {
5761
}
5862
),
5963
path: parsedUrl(
60-
function (parts) {
64+
function (parts, set) {
65+
if (set) {
66+
var oldUrl = absUrl();
67+
$rootScope.$apply(
68+
function () {
69+
parts.pathname = set;
70+
redirectTo = absUrl();
71+
$rootScope.$broadcast(
72+
'$locationChangeSuccess',
73+
redirectTo,
74+
oldUrl
75+
);
76+
}
77+
);
78+
}
6179
return parts.pathname;
6280
}
6381
),
@@ -87,6 +105,9 @@ function registerModule(context) {
87105
else {
88106
request = newRequest;
89107
}
108+
},
109+
getRedirectTo: function () {
110+
return redirectTo;
90111
}
91112
};
92113
}

0 commit comments

Comments
 (0)