Skip to content

Commit

Permalink
Merge pull request openshift#2255 from ggreer/fix-js-basepath
Browse files Browse the repository at this point in the history
Fix console running under a path other than /.
  • Loading branch information
ggreer authored Apr 24, 2018
2 parents c76db25 + d48c29a commit 550f76c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
9 changes: 6 additions & 3 deletions frontend/public/components/nav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import * as routingImg from '../imgs/routing.svg';
import * as appsLogoImg from '../imgs/apps-logo.svg';
import * as routingActiveImg from '../imgs/routing-active.svg';
import * as appsLogoActiveImg from '../imgs/apps-logo-active.svg';
import { history } from './utils';
import { history, stripBasePath } from './utils';


const stripNS = href => href.replace(/^\/?k8s\//, '').replace(/^\/?(cluster|all-namespaces|ns\/[^/]*)/, '').replace(/^\//, '');
const stripNS = href => {
href = stripBasePath(href);
return href.replace(/^\/?k8s\//, '').replace(/^\/?(cluster|all-namespaces|ns\/[^/]*)/, '').replace(/^\//, '');
};

class NavLink extends React.PureComponent {
static isActive () {
Expand Down Expand Up @@ -138,7 +141,7 @@ const NavSection = connect(navSectionStateToProps)(
const { activeNamespace, location, children } = this.props;

if (!children) {
return location.startsWith(this.props.activePath);
return stripBasePath(location).startsWith(this.props.activePath);
}

const resourcePath = location ? stripNS(location) : '';
Expand Down
12 changes: 6 additions & 6 deletions frontend/public/components/utils/link.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const basePathPattern = new RegExp(`^/?${window.SERVER_FLAGS.basePath}`);

export const namespacedPrefixes = ['/search', '/overview', '/k8s'];

export const stripBasePath = path => {
path = path.replace(basePathPattern, '/');
path = path.replace(/^\/?k8s\//, '');
return path;
};
export const stripBasePath = path => path.replace(basePathPattern, '/');

export const isNamespaced = path => namespacedPrefixes.filter(p => path.startsWith(p)).length > 0;
export const getNSPrefix = path => {
path = stripBasePath(path);
return namespacedPrefixes.filter(p => path.startsWith(p))[0];
};

export const getNamespace = path => {
path = stripBasePath(path);
const split = path.split('/').filter(x => x);

if (split[1] === 'all-namespaces') {
Expand Down
15 changes: 5 additions & 10 deletions frontend/public/ui/ui-actions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as _ from 'lodash-es';
import store from '../redux';
import { history } from '../components/utils/router';
import { ALL_NAMESPACES_KEY } from '../const';
import { namespacedPrefixes, isNamespaced } from '../components/utils/link';
import { getNSPrefix } from '../components/utils/link';
import { allModels } from '../module/k8s/k8s-models';

// URL routes that can be namespaced
Expand All @@ -29,16 +28,12 @@ export const formatNamespacedRouteForResource = (resource, activeNamespace=getAc
};

export const formatNamespaceRoute = (activeNamespace, originalPath, location) => {
if (!isNamespaced(originalPath)) {
return originalPath;
}

const prefix = _.find(namespacedPrefixes, p => originalPath.startsWith(p));
const prefix = getNSPrefix(originalPath);
if (!prefix) {
throw new Error(`no prefix for path ${originalPath}?`);
return originalPath;
}

originalPath = originalPath.substr(prefix.length);
originalPath = originalPath.substr(prefix.length + window.SERVER_FLAGS.basePath.length);

let parts = originalPath.split('/').filter(p => p);
let previousNS = '';
Expand Down Expand Up @@ -90,7 +85,7 @@ export const UIActions = {
// broken direct links and bookmarks
if (namespace !== getActiveNamespace()) {
const oldPath = window.location.pathname;
if (isNamespaced(oldPath)) {
if (getNSPrefix(oldPath)) {
history.pushPath(formatNamespaceRoute(namespace, oldPath, window.location));
}
}
Expand Down
13 changes: 11 additions & 2 deletions frontend/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let config: webpack.Configuration = {
],
output: {
path: path.resolve(__dirname, 'public/dist'),
publicPath: '/static/',
publicPath: 'static/',
filename: '[name]-bundle.js',
chunkFilename: '[name]-[chunkhash].js',
},
Expand Down Expand Up @@ -83,12 +83,21 @@ let config: webpack.Configuration = {
],
},
{
test: /\.(png|jpg|jpeg|gif|svg|woff2?|ttf|eot|otf)(\?.*$|$)/,
test: /\.(png|jpg|jpeg|gif|svg)(\?.*$|$)/,
loader: 'file-loader',
options: {
name: 'assets/[name].[ext]',
},
},
// Something's shoving 'static/' onto web fonts already. I have no clue what. - ggreer
{
test: /\.(woff2?|ttf|eot|otf)(\?.*$|$)/,
loader: 'file-loader',
options: {
name: 'assets/[name].[ext]',
publicPath: './',
},
},
]
},
optimization: {
Expand Down

0 comments on commit 550f76c

Please sign in to comment.