Correct preview route for fastly

- use more explicit route
- handle fastly error in the callback correctly
- more succinct parsing of the URL path
This commit is contained in:
chrisgarrity 2018-05-02 17:01:52 -04:00
parent d1420862ea
commit 6df58e41fd
3 changed files with 11 additions and 23 deletions

View file

@ -176,7 +176,7 @@ async.auto({
if (err) throw new Error(err); if (err) throw new Error(err);
if (process.env.FASTLY_ACTIVATE_CHANGES) { if (process.env.FASTLY_ACTIVATE_CHANGES) {
fastly.activateVersion(results.version, function (e, resp) { fastly.activateVersion(results.version, function (e, resp) {
if (err) throw new Error(e); if (e) throw new Error(e);
process.stdout.write('Successfully configured and activated version ' + resp.number + '\n'); process.stdout.write('Successfully configured and activated version ' + resp.number + '\n');
if (process.env.FASTLY_PURGE_ALL) { if (process.env.FASTLY_PURGE_ALL) {
fastly.purgeAll(FASTLY_SERVICE_ID, function (error) { fastly.purgeAll(FASTLY_SERVICE_ID, function (error) {

View file

@ -193,7 +193,7 @@
}, },
{ {
"name": "preview", "name": "preview",
"pattern": "^/preview/(*)/?$", "pattern": "^/preview(/editor|(/\\d+(/editor|/fullscreen)?)?)?/?$",
"routeAlias": "/preview/?$", "routeAlias": "/preview/?$",
"view": "preview/preview", "view": "preview/preview",
"title": "Scratch 3.0 Preview" "title": "Scratch 3.0 Preview"

View file

@ -54,31 +54,19 @@ class Preview extends React.Component {
} }
} }
initState () { initState () {
let pathname = window.location.pathname.toLowerCase(); const pathname = window.location.pathname.toLowerCase();
const params = { const parts = pathname.split('/').filter(Boolean);
// parts[0]: 'preview'
// parts[1]: either :id or 'editor'
// parts[2]: undefined if no :id, otherwise either 'editor' or 'fullscreen'
return {
editable: false, editable: false,
favoriteCount: 0, favoriteCount: 0,
inEditor: false, inEditor: parts.indexOf('editor') !== -1,
isFullScreen: false, isFullScreen: parts.indexOf('fullscreen') !== -1,
loveCount: 0, loveCount: 0,
projectId: null projectId: parts[1] === 'editor' ? null : parts[1]
}; };
if (pathname[pathname.length - 1] === '/') {
pathname = pathname.substring(0, pathname.length - 1);
}
const path = pathname.split('/');
if (path[path.length - 1] === 'editor' || path[path.length - 1] === 'preview') {
params.inEditor = true;
path.pop();
}
if (path[path.length - 1] === 'fullscreen') {
params.isFullScreen = true;
path.pop();
}
if (/^\d+$/.test(path[path.length - 1])) {
params.projectId = path.pop();
}
return params;
} }
handleFavoriteToggle () { handleFavoriteToggle () {
this.props.setFavedStatus( this.props.setFavedStatus(