Angular2 routing redirects to wrong url - .htaccess

I have an angular2 application in a subfolder /draft on a apache server.
In order to get it working I added this .htaccess in /draft:
Options Indexes FollowSymLinks
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /draft/index.html [L]
And added this to /draft/index.html in the head:
<base href="draft/">
My components and other typescript files are in /draft/app/,
and my /draft/systemjs.config.js looks like this:
(function (global) {
System.config({
paths: {
'npm:': '/draft/node_modules/'
},
map: {
app: '/draft/app',
'#angular/core': 'npm:#angular/core/bundles/core.umd.js',
'rxjs': 'npm:rxjs',
// ...
},
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
This all works fine, but when I try to add routing something goes wrong.
When the user goes to /draft/foo, I want the component FooComponent to be shown.
But when the user goes to /draft/foo, the FooComponent is shown as expected, but for some reason the url is changed to /draft/draft/draft/foo.
This is my /draft/app/app.routing.js:
// ...
const appRoutes: Routes = [
{
path: 'draft/foo',
component: FooComponent
}
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
I added the routing const to the imports array of my AppModule, /draft/app/app.module.ts:
// ...
#NgModule({
imports: [ BrowserModule, HttpModule, routing ],
declarations: [ AppComponent, FooComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
/draft/app/components/AppComponent.ts looks like this:
import {Component} from '#angular/core';
#Component({
selector: 'my-app',
template: '<router-outlet></router-outlet>'
})
export class AppComponent {}
So why is /draft/foo redirected to /draft/draft/draft/foo and what can I do to stop it?

Apparently the base was incorrect.
With the following base everything works.
<base href="http://localhost/draft/" />
To make it more dynamic I replaced it with this:
<script>
document.write('<base href="' + document.location + '" />');
</script>
But this only works when using the hash location strategy because otherwise the Route path is always ' '. So I ended up using the following:
<script>
document.write('<base href="' + document.location.protocol + '//' + document.location.host + '/draft/' + '" />');
</script>

Related

ReferenceError: document is not defined (React SSR, Webpack)

I'm trying to do server-side rendering my react app but it doesn't work. It shows document not define when trying to run. This error only shows when I used CSS file in app.js file. Though client-side render working properly.
//My App.js
import React from 'react';
import { renderRoutes } from 'react-router-config';
import ErrorBoundary from './validation/ErrorBoundry';
import './assets/css/bootstrap.min.css';
import './assets/scss/main.css';
const App = ({ route }) => {
return (
<div className="App">
<ErrorBoundary>{renderRoutes(route.routes)}</ErrorBoundary>
</div>
);
};
export default App;
//webpack.base.js
module.exports = {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
],
}
]
},
}
error screenshot
The document object is the root node of the HTML document.
This object does not exist in node application.
And the way to handle it in node application can be done by using jsdom.
jsdom is a pure-JavaScript implementation of many web standards,
notably the WHATWG DOM and HTML Standards, for use with Node.js
That mistake happens because, Webpack transforms the CSS to the following code:
var css_main_css = document.createElement("style");
css_main_css.innerHTML = "*css*";
document.querySelector("head").appendChild(css_main_css);

Vue Website Returns "Cannot Get /path" On All Pages EXCEPT Index

I am using Vue on Node.js to host my website on an AWS EC2 instance. I dont have an index node.js file, just the vue-router file. I use AWS CloudFront to bind my certificate to my traffic. The problem is that everytime i access the site through the server's link, the site works perfectly, but whenever i access it through the cloud-front link, only the index of the website will show up. No /about or /contact; instead it returns Cannot GET /about.
My Router:
import Vue from 'vue';
import Router from 'vue-router';
import VueCookies from 'vue-cookies';
import Home from './views/Home.vue';
import NotFound from './views/NotFound.vue';
Vue.use(Router);
Vue.use(VueCookies);
VueCookies.config('7d');
VueCookies.set('theme', 'default');
VueCookies.set('unique', Date.now());
VueCookies.set('rwapi-uuid', `v3-${Date.now()}-x9k0`)
export default new Router({
mode: 'history',
routes: [
{ path: '/', name: 'INDEX', component: Home },
{ path: '/about/', name: 'ABOUT', component() { return import('./views/About.vue'); } },
{ path: '/portfolio', name: 'PORTFOLIO', component() { return import('./views/Port.vue'); } },
{ path: '/contact', name: 'CONTACT', component() { return import('./views/Contact.vue'); } },
{ path: '/login', name: 'LOGIN', component() { return import('./views/Login.vue'); } },
{ path: '/404', component: NotFound },
{ path: '*', redirect: '/404' },
],
});
I have already tried to add the historyApiFallback: true to my webpack config but it had no effect.
According to Vue Router's documentation, when using your router in history mode, your webserver requires additional configuration.
I don't exactly know how do EC2 instances work, but if you don't have a webserver proxying all your requests to index.html, Vue-router will not be able to handle the other requests.

Problems with require and a html file

I'm trying to require a html template file as part of my KnockoutJS component registration. According to Chrome's network tab, all the other files load fine except login-template.html which appears as
file:///home/leon/Dev/KnockOutJS/Component%20Example/js/lib/text.js
in my local file path. What Am I doing wrong here?
requirejs.config({
baseUrl: 'js/lib',
paths: {
app: '../app',
jquery: 'jquery-3.3.1',
knockout: 'knockout-3.4.2',
}
});
requirejs(['jquery', 'knockout', 'app/login-component', 'text!app/login-template.html'],
function ($, ko, loginComponent, loginTemplate) {
ko.components.register('login-component', {
viewModel: { require: loginComponent },
template: { require: loginTemplate }
});
ko.applyBindings();
});
You need to install the text plugin from https://github.com/requirejs/text

Change hash-url to hash-bang url using angular4

I am using angulr4 this is my URL http://localhost/#/login.html I want to change this url to http://localhost/#!/login.html. I found solution for angularjs but not for angular4. I am using "Prerender Node" for SEO regarding this is link https://www.npmjs.com/package/prerender-node
Thanks in advance
What you are after is APP_BASE_HREF. In your routing module app-routing.module.ts add to your module providers array { provide: APP_BASE_HREF, useValue: '!' } and import import { APP_BASE_HREF } from '#angular/common'; at the top of the file. It looks like you are already using HashLocationStrategy.
Example app-routing.module.ts:
import { NgModule } from '#angular/core';
import { Routes, RouterModule} from '#angular/router';
import { APP_BASE_HREF } from '#angular/common';
const routes: Routes = [
{ path: '**', redirectTo: '404-not-found' }
];
#NgModule({
imports: [
RouterModule.forRoot(routes, {
useHash: true
})
],
exports: [ RouterModule ],
providers: [
{ provide: APP_BASE_HREF, useValue: '!' }
]
})
export class AppRoutingModule {}
Further reading Angular 4 documentation
PathLocationStrategy:
PathLocationStrategy is a LocationStrategy used to configure the
Location service to represent its state in the path of the browser's
URL.
If you're using PathLocationStrategy, you must provide a APP_BASE_HREF
or add a base element to the document. This URL prefix that will be
preserved when generating and recognizing URLs.
For instance, if you provide an APP_BASE_HREF of '/my/app' and call
location.go('/foo'), the browser's URL will become
example.com/my/app/foo.
Similarly, if you add <base href='/my/app'/> to the document and call
location.go('/foo'), the browser's URL will become
example.com/my/app/foo.
HashLocationStrategy:
You can go old-school with the HashLocationStrategy by providing the
useHash: true in an object as the second argument of the
RouterModule.forRoot in the AppModule.
APP_BASE_HREF:
... APP_BASE_HREF token represents the base href to be used ...
a string representing the URL prefix that should be preserved when generating and recognizing URLs

How to structure a Vue 2.0 app for server-rendered lazy routes?

I attempted to modify vue-hackernews-2.0 to support lazy-loaded routes using Webpack's code-splitting feature as per instructions found at these links:
https://router.vuejs.org/en/advanced/lazy-loading.html
http://vuejs.org/guide/components.html#Async-Components
However, I ran into some issues. When I loaded the app in the browser, all suggested variations of syntax triggered Module not found errors on the server-side when attempting to load in the server-side chunks.
Given this wrapper around the code-split points in router.js...
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
// INSERT CODE-SPLIT POINT SYNTAXES HERE (they are below)
export default new Router({
mode: 'history',
routes: [{
path: '/',
component: Home
}, {
path: '/foo',
component: Foo
}]
})
All of these variations of syntax threw the Module not found error:
Variation 1:
const Home = () => System.import('./views/Home.vue')
const Foo = () => System.import('./views/Foo.vue')
Variation 2:
const Home = (resolve) => require(['./views/Home.vue'], resolve)
const Foo = (resolve) => require(['./views/Foo.vue'], resolve)
Variation 3:
const Home = (resolve) => {
require.ensure(['./views/Home.vue'], () => {
resolve(require('./views/Home.vue'))
})
}
const Foo = (resolve) => {
require.ensure(['./views/Foo.vue'], () => {
resolve(require('./views/Foo.vue'))
})
}
The error message was always along the lines of:
(note: this error is adapted from a small reproduction I made of the issue, not from the hackernews example)
Error: Cannot find module './0.server.js'
at Function.Module._resolveFilename (module.js:440:15)
at Function.Module._load (module.js:388:25)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Function.requireEnsure [as e] (__vue_ssr_bundle__:42:25)
at Home (__vue_ssr_bundle__:152:30)
at /Users/razorbeard/projects/vue-2-ssr/node_modules/vue-router/dist/vue-router.js:1421:19
at iterator (/Users/razorbeard/projects/vue-2-ssr/node_modules/vue-router/dist/vue-router.js:1277:5)
at step (/Users/razorbeard/projects/vue-2-ssr/node_modules/vue-router/dist/vue-router.js:1213:9)
at step (/Users/razorbeard/projects/vue-2-ssr/node_modules/vue-router/dist/vue-router.js:1217:9)
I tried adapting my code to use the suggestions offered at Server-side react with webpack 2 System.import, but those did not work either.
I read a post that described configuring a build-time global variable using Webpack's DefinePlugin that allowed me to inspect whether the code was running on the server or on the client - this allows me to code-split on the client, but just bundle everything in on the server.
In the server webpack config:
{
...
plugins: [
new webpack.DefinePlugin({
BROWSER_BUILD: false
})
]
...
}
In the client webpack config:
{
...
plugins: [
new webpack.DefinePlugin({
BROWSER_BUILD: true
})
]
...
}
Then, in the same wrapper-snippet as above for the router.js file, I used this variation of syntax:
const Home = BROWSER_BUILD ? () => System.import('./views/Home.vue') : require('./views/Home.vue')
const Foo = BROWSER_BUILD ? () => System.import('./views/Foo.vue') : require('./views/Foo.vue')
This made rendering work - partially. Navigating directly to the app in the browser (and respective routes) server-rendered the correct UI. Clicking around, vue-router's client-side logic took me to the right UI. Everything seemed hunky-dory - until I opened DevTools:
The same issue also occurs if a module is loaded lazily as a subcomponent of a route:
<template>
<div class="page">
<heading></heading>
</div>
</template>
<script>
const Heading = BROWSER_BUILD ? () => System.import('./Heading.vue') : require('./Heading.vue')
export default {
components: {
Heading
}
}
</script>
I tried asking for some help in the official Vue forum, but came up empty: http://forum.vuejs.org/t/2-0-help-needed-with-server-rendered-lazy-routes/906
Thinking this might be a bug with vue-router, I opened an issue there: https://github.com/vuejs/vue-router/issues/820
Unfortunately, I wasn't able to find a solution.
So, I put together a small repo that reproduces the issue: https://github.com/declandewet/vue2-ssr-lazy-error
I have a hunch that the actual problem might be coming from https://www.npmjs.com/package/vue-server-renderer.
I'm really stuck on this and am used to how easy it is to do in react - and would really appreciate any help/tips/direction towards a solution!
Here is the webpack config from the reproduction repo for convenience:
import fs from 'fs'
import path from 'path'
import webpack from 'webpack'
import validate from 'webpack2-validator'
import { dependencies } from './package.json'
let babelConfig = JSON.parse(fs.readFileSync('./.babelrc'))
/* turn off modules in es2015 preset to enable tree-shaking
(this is on in babelrc because setting it otherwise causes issues with
this config file) */
babelConfig.presets = babelConfig.presets.map(
(preset) => preset === 'es2015' ? ['es2015', { modules: false }] : preset
)
const babelOpts = {
...babelConfig,
babelrc: false,
cacheDirectory: 'babel_cache'
}
const SHARED_CONFIG = {
devtool: 'source-map',
module: {
loaders: [{
test: /\.vue$/,
loader: 'vue'
}, {
test: /\.js$/,
loader: 'babel',
exclude: 'node_modules',
query: babelOpts
}]
},
resolve: {
modules: [
path.join(__dirname, './src'),
'node_modules'
]
}
}
const SERVER_CONFIG = validate({
...SHARED_CONFIG,
target: 'node',
entry: {
server: './src/server.js',
renderer: './src/renderer.js'
},
output: {
path: path.join(__dirname, './dist'),
filename: '[name].js',
chunkFilename: '[id].server.js',
libraryTarget: 'commonjs2'
},
plugins: [
new webpack.DefinePlugin({
BROWSER_BUILD: false,
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
}),
new webpack.BannerPlugin({
banner: 'require("source-map-support").install();',
raw: true,
entryOnly: false
})
],
externals: Object.keys(dependencies)
})
const CLIENT_CONFIG = validate({
...SHARED_CONFIG,
entry: {
app: './src/client.js',
vendor: ['vue']
},
output: {
path: path.join(__dirname, './dist/assets'),
publicPath: '/',
filename: 'bundle.js'
},
plugins: [
new webpack.DefinePlugin({
BROWSER_BUILD: true,
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: 'vendor.js'
})
]
})
export default [SERVER_CONFIG, CLIENT_CONFIG]
EDIT: Noticing that in React, we use match on the client to get the right route config for the current view, I decided to inspect what components were getting matched using app.$router.getMatchedComponents() and found something interesting:
Server Entry:
import app from './app'
export default (context) => {
// using app.$router instead of importing router itself works
// (not sure why the hacker-news example imports the router module instead...)
app.$router.push(context.url)
const components = app.$router.getMatchedComponents()
console.log('server-side', components)
return Promise.all(components.map((component) => component))
.then(() => app)
}
When navigating to the home page, this logs to the terminal:
server-side [ { __file: '/Users/razorbeard/projects/vue-2-ssr/src/views/Home.vue',
render: [Function],
staticRenderFns: [ [Function] ] } ]
Client Entry:
import app from './app'
const components = app.$router.getMatchedComponents()
console.log('client-side', components)
// kickoff client-side hydration
Promise.all(components.map((component) => Promise.resolve(component)))
.then(() => app.$mount('#app'))
When navigating to the home page, this logs to the devtools console:
client-side []
As you can see, no components are getting matched on the client side.

Resources