Preact and Parcel - Routing issue after bundling - single-page-application

I've been seeking for a solution to this for over a month now.
I've built a personal landing page using parcel-bundler and preact. I am also using preact-router to handle my routing.
Here's some code.
Main App Component
class App extends Component {
render(props) {
return (
<div id='main'>
{/* Components outside of router persist
throughout all pages */}
<Header />
<Nav tabs={tabs} />
<Router>
<About default path='/about' />
<Skills path='/skills' />
<Books path='/books' />
</Router>
</div>
)
}
}
Nav Component
const Nav = ({ tabs }) =>
<nav>
{tabs.map(t => (
<p>
<Link activeClassName='active' href={ t.path }>{ t.title.toUpperCase() }</Link>
</p>
))}
</nav>
tabs.json
[
{
"title": "About",
"path": "/about"
},
{
"title": "Skills",
"path": "/skills"
},
{
"title": "Books",
"path": "/books"
}
]
So, whenever I run the page live using parcel's server, everything works as expected: I click on a tab, and the respective page/component gets rendered.
However, whenever I run parcel build blah blah, open my index.html and click on any tab, I get the following https://i.imgur.com/71ogrFA.png (screenshot of browser window when I click on a tab), and the URL bar of the browser changes to file:///C:/{route_name}.
I'm trying to understand why it is that my page works when I run it on parcel's live server, and why it doesn't work after build? It seem like, after build, clicking any tabs causes the browser to search for the page in the file system? Is that case? Ultimately, how can I fix this?

A leading slash in URLs makes them absolute to the root.
If the site is transported via HTTP, the root is the domain: <a href="/bar"> placed on a site with the URL http://example.com/foo/bar would point to http://example.com/bar.
However, in case of the file protocol that is used when you open a file from your PC in the browser, the root is the root folder of the current drive, usually C:. So if you opened the same file locally on file:///C:/some/path/foo/bar, the link would point to file:///C:/bar.
Most importantly, this means that this error is only present during local development. It won't occur once your site is hosted on an actual server.
You can run a local HTTP server with tools like XAMPP or MAMP to make sure the links work before deployment.
The parcel development server is also running a local HTTP server, that's why the links work fine there.

Related

Netlify react router redirect configuration breaks css links

Google crawl-bot is triyng to access https://my-website.com/.../index.231e3e.css
But, because my app is a react-router app, I have this redirect rule configured:
[[redirects]]
from = "/*"
to = "/"
status = 200
force = false
which redirects the css path to the main home page (loading the website, rather than a css file).
I'm not sure how the CSS gets loaded in the background when you do visit the normal website - because the styles load in fine - but when I directly access this css link, it redirects to the home page.
I'm wondering how I can fix this redirect issue allowing google indexer to properly style my page (currently the page looks broken in the rendered screenshots, due to the missing css files which failed to download).
It seems now the css loads (maybe 1 time error) but the web page is still rendered incorrectly with no css

ERR_SSL_PROTOCOL_ERROR with image sources Razor pages

I am currently working on an ASP.NET Core application with Razor pages. The API endpoints are hosted in pre-production server. When I get an image link which is stored in the same server as the apis, through api, and want to display it in the razor page the image can't be shown due to ERR_SSL_PROTOCOL_ERROR. When I inspect element and get the src of the image tag and open it in a new tab it will open. Also if I use that source in a html page it still opens. Since the pre-production server does not have ssl certificate configured, is there any chances the PageModel abstract class of the razor page to have any validation on that, since it is working on plain html?
EDIT:
I am providing from the API the image tag as html:
<a href ="http://imagesourceIPExample">
<img src="http://imagesourceIPExample">
</a>
I am getting that string from an ajax request
$.ajax({
//ajax call params,
success: function(data){
$(`#messageDiv`).append(data.message);
}
}).
Now when I inspect the page and click the link in the anchor tag I would open fine. Also if I click it from the view. But the image isn't showing and the console shows that SSL_PROTOCOL_ERROR. And image link is http.
First of all, razor PageModel doesn't validate image loading.
It seems like you are working with secured https site and then when you append plain http image links, browser refuses to load them due to its Mixed Content policy. But when you load image in a separate window it will load just fine because the "mixed content" problem is gone, you load only http resource, no https involved.
Consider converting you image links to https, it should fix the problem.

Link to a non-gatsby html page on same domain without Gatsby redirecting to 404

I have a Gatsby blog. I am writing a set of tutorial posts for coding games in create-react-app. In my Gatsby post markdown, I'm trying to link to the built version of the demo react app for that post hosted on the same server, but Gatsby keeps giving me a 404 page.
I am using nginx. All the contents of the public folder resulting from gatsby build sit in the var/www/html directory of my webserver.
The create-react-app build sits inside var/www/html/tutorials/01/, and has its own index.html file.
In my markdown I have tried both these formats of links: view the code [here](/tutorials/01/) and view the code [here](//165.227.94.249/tutorials/01/).
If you're viewing the post in your browser at //165.227.94.249/posts/tutorial-01 and click on the link, it'll take you to //165.227.94.249/tutorials/01/ but display a 404 page. But if you refresh the browser at the same URL, the working react app will be served.
How do I keep Gatsby from overriding this request and showing a 404 page instead of just letting the web server serve up the index.html file that exists at that url?
The internal links in your markup are automatically converted into Gatsby Link while you build. Gatsby Link is only for internal ressources from the Gatsby build. Your create-react-app app is on the same server but outside the Gatsby build process.
From the documentation:
This component is intended only for links to pages handled by Gatsby.
For links to pages on other domains or pages on the same domain not
handled by the current Gatsby site, use the normal element.
I think using the full URL inside the markup including https might be successful: https://165.227.94.249/tutorials/01/
PS: When I tried I got a connection timeout from your server.
You should try to disable gatsby-plugin-catch-links. In case this doesn't works try to catch the onClick event, cancel the event and trigger your own redirect.
function processExternalSameDomainLink(event) {
window.location.href = url
event.stopPropagation()
event.preventDefault()
return false
}
<a href={newTo} onClick={processExternalSameDomainLink}>

URL's Path are not working when deployed to IIS Server System

I have problem that when i deployed my ASP.NET MVC project to IIS Server System, the images url's add from CSS they are not taking exact path and in Angularjs unable send post or get request.
CSS sample code
<div class="container" style="background-image:url('../../Images/img/product-1.jpg')">
<div class="text-left">Welcome</div>
</div>
AngularJS sample code
var app=angular.module('myApp', ['ngRoute']);
app.controller('myCtrl', ['$scope', '$http', function($scope, $http){
$http({
method:"POST",
url:"/controller/action",
data:{id:1}
}.then(function(response){
console.log(response)
}, function(error){
console.log(error)
});
]});
so these are sample code what i have written in my project.
i have search internet i have found some solutions like
Installing StaticContent from add or remove features for IIS
changing application pool for authentication.
remove staticContent tags in web.config.
i have tried these thing evening i am unable solve my issue.
my error are:
GET http://18.xxx.xxx.xxx/controller/action 404 (Not Found)
GET http://18.xxx.xxx.xxx/Images/img/bg-img/top.jpg 404 (Not Found)
actual url should load like this
GET http://18.xxx.xxx.xxx/myApplication/controller/action 404 (Not Found)
GET http://18.xxx.xxx.xxx/myApplication/Images/img/bg-img/top.jpg 404 (Not Found)
Change the style mentioned in below style.
Notes: change the directly level.
<div class="container" style="background-image:url('~/Images/img/product-1.jpg')">
<div class="text-left">Welcome</div>
<div class="container" style="background-image:url('#Url.Content("~/Images/img/product-1.jpg")')">
<div class="text-left">Welcome</div>
Explanation of ~ vs /:
/ - Site root
~/ - Root directory of the application

Firebase, url with variables redirects to main page

I have a link that looks like this:
https://mywebsite.com/#/new-account?jk=-LOLgLiyfxANW-ojMKrf
jk is the variable that I would like to read and use on this page. The variable will change for each user.
The problem is that when this link is clicked, the user is redirected to the main page of the application. The app is a PWA developed using Ionic. I checked my code carefully and there is nothing that would cause such redirect behavior.
So far I tried uploading the app again with deselecting "single page" option during Firebase Init but the problem persists.
thanks
I contacted firebase support and they suggested I solve it by including this in firebase.json within the hosting tag:
"redirects": [{
"source": "/#/new-account?:vars*",
"destination": "https://mywebsite.com/#/new-account?:vars*",
"type": 301
}]
Firebase hosting captures whatever text is after : and transfers it to the destination. Otherwise, it creates a 404 for any link that doesn't exactly match the URL of existing pages.
Maybe there is a more elegant way to do this but it worked well for my situation. More info is available at this link:
https://firebase.google.com/docs/hosting/full-config#redirects

Resources