Serve top level domain as static html page in next.js? - html

In next.js you can use the "public" directory in your root directory to serve static assets. I have my index.html file in this directory because I want my landing page to be purely static.
However, when using next or next start, I cannot access my landing page at http://localhost:3000/. I can only access my landing page at "http://localhost:3000/index.html".
Is there a way to setup next.js, so the top level domain (http://localhost:3000) will serve my landing page?

You can add rewrites to your next.config.js. To allow the url mysite.com/ to point to /public/index.html, I added this to my config (with the content in my /public directory):
rewrites: async () => {
return [
{
source: "/",
destination: "/index.html",
}
]
}
Note: index.jsx/.tsx will take precedence over files in /public
Source: https://nextjs.org/docs/api-reference/next.config.js/rewrites

Next.js router serves only pages exported from .js, .ts and .tsx in pages directory.
You would need a custom server that would serve anything outside of that.
Custom Server
Also, if you use a reverse proxy like nginx you could handle this request by the server before it hits Node.js.
What is the use case of it? If the landing page is just a .html file, you can wrap it in a Next.js page. With server side rendering, the page will be pre-rendered on the server and served the same as it was just a .html file.
Alternatively, if you can't convert HTML to JSX you could use dangerouslySetInnerHTML to set raw HTML code to React's render().
However, beware of cross-site scripting (XSS) attacks.

Related

How to change my url from https://example.com/name.html to https://example.com/name?

How to change my url from https://example.com/name.html to https://example.com/name?
I am using Firebase Hosting to host my website. The site is made in HTML and javascript using bootstrap studio.
After reading various solution online, i know that '.htaccess' file can be used to change the url but none of the solution provide a way to do it with Firebase Hosting.
Firebase Hosting has a setting called cleanUrls that controls the presence/absence of the HTML extension. See the documentation on controlling .html extensions:
The cleanUrls attribute allows you to control whether or not URLs should include the .html extension.
When true, Hosting automatically drops the .html extension from uploaded file URLs. If an .html extension is added in the request, Hosting performs a 301 redirect to the same path but eliminates the .html extension.
Specify the inclusion of .html extensions by including a cleanUrls attribute within hosting in your firebase.json file. For example:
"hosting": {
// ...
// Add the "cleanUrls" attribute within "hosting"
"cleanUrls": true
}

How do I import an HTML template into a Vue.js project?

I want to import a template that I downloaded off the internet into my vue project. The template has its own HTML, CSS, and Javascript files. How do I use vue-router to access the index.html that is in that project without converting them into vue components?
I'm not sure if v-html is the way to go since I have to import and then convert the HTML into a huge string.
I'm doing this for my landing page so it doesn't need to access Vuex and all the other complexities. I just need it to be displayed as a normal HTML.
Here is my folder structure
What you can do is, serve the landing page off the root of your webapp, and serve vue's dist folder from a subfolder.
For example, your landing is served from abc.com/index.html and your vue app is served from abc.com/app/index.html, and the you can use a simple anchor to send the user to the vue dist folder.
The actionable steps would be -
1. Set vue's dist folder to public/app
2. When finally deploying, set webapp root to public and make sure the index.html inside public is the one of the landing template you downloaded.
So, now, when user will go to /, your downloaded landing will be shown, but when they go to /app, vue's build folder's index.html will be returned, thus taking him to the vue app.

react-create-app static html page in public folder

I'm using react-create-app for creating a single page application. Now I have to create a legal page with static text, which I don't want to be part of the main application.
So I've added a html-page to the public folder, which then gets copied to the build directory. Unfortunately I can't link to that page in production, as it always loads the main react application, even with the correct URL for the static html page.
I believe it has something to do with the service worker. Does anybody has an idea on how to fix this?
Thanks :)
If you use react router, make a stateless component with the static html and just route as you would.
If you have a node or any other server serving your content, just make a route on the server directly to the other html file
The service worker don’t relate to this. The service worker is a cache manager

Serving static files with node express

Background
I'm serving a webapp on https://jienan.xyz/m, in Apache I redirect the link to localhost:3001/memo with
ProxyPass /m http://localhost:3001/memo
ProxyPassReverse /m http://localhost:3001/memo
I'm listening to port 3001, with express. If no params or queries attached, ./html/web_portal.html will be returned. I placed app logo, favicon and other imgs that I hoped to served statically under ./html/icon/ with express.static:
app.use(express.static(__dirname + '/html/'));
In the web portal html, I refer the links of img with <img ... src="/icon/ic_pullsh.png"/>
Problem
After all these implementations, the imgs can't be loaded when accessed from domain. I can see the img has the link https://jienan.xyz/icon/ic_pullsh.png.
If I use the ip address http://130.211.211.220:3001/memo, the img can be seen, with the address of http://130.211.211.220:3001/icon/ic_pullsh.png.
Question
How can I change the implementations to serve the files statically, to make it visible from domain?
What I found is not a very smart answer in my mind.
Since I redirect jienan.xyz/m to localhost:3001/memo, the correct format of link to static file should be https://jienan.xyz/m/icon/ic_pullsh.png, and it will be redirected to http://130.211.211.220:3001/memo/icon/ic_pullsh.png.
What happens is it will look in the ./html/memo/icon/ folder for that png. So I have to declare to serve static file under /html/ in express, and put all the files under /html/memo/.
Maybe it's not a good idea to redirect request to /memo at first

Express + AngularJS + HTML: ng-include not working (404 - Page not found error)

I am new to AngularJS. I am trying to use ng-include to include an external HTML page in my main HTML page. But the problem is I am not able to include it and getting 404. Following is the folder structure and the code,
Project Folder Structure:
buttonClick.jade (This is the starting page.)
doctype html
html(ng-app)
head
link(rel='stylesheet', href='/stylesheets/bootstrap.min.css')
link(rel='stylesheet', href='/stylesheets/style.css')
script(src='/javascripts/angular.min.js')
body(class="mainPage")
//include footer.html
include pageinclude.html
pageinclude.html
<div>
<div>Include Page Demo</div>
<div ng-include="'footer.html'"></div>
</div>
Note:
1) When I include footer.html page directly in the .jade file then it is working fine. But when I do the same using ng-include in the HTML file it does not work.
2) I have also tried the following ng-include ways,
<div ng-include="'footer.html'"></div>
<ng-include src="'footer.html'"></ng-include>
ng-include is a client side include and as such the path to the included html file is relative to the client perception of the url.
Since jade is abstracting your folder structure and does not provide direct access to your views folder you should probably put the included html file on the public folder just as any externaly accessible file.
When you include the footer in your .jade file (As per Note 2) you are doing a server side include which uses the server directory structure.
I also had similar problem and I'm also new in Angular and Node :)
I'm not sure that my solution is safe, so if itsn't let me know about that.
I've solved it by expose partial directory with express.static middleware.
So add something like that to the app.js:
app.use(require('less-middleware')(path.join(__dirname, 'public'))); //common
app.use('/views', express.static(__dirname + '/views')); //serve views directory as assets
Then you can access your partial from client side:
<div ng-include="'/views/footer.html'"></div>