How to get SSR? - next.js

Implementing a nextjs 13 app with the app directory and the Client and Server Component.
When displaying the source HTML, my body is empty and only contains scripts and JSON.
<!DOCTYPE html>
<html id="__next_error__">
<head>
<script src="/_next/static/chunks/polyfills.js" nomodule=""></script>
</head>
<body>
<script src="/_next/static/chunks/webpack.js" async=""></script>
<script src="/_next/static/chunks/main-app.js" async=""></script>
</body>
</html>
The JSON/script part is after this html and contains the whole page. As you can see, my body is empty, which I would like to avoid for SEO reasons.
When trying with the former manner with a page, I have the html displaying correctly in the body in an html tag.
Did I miss something that prevents SSR in nextjs 13?
In my Layout, I have a Client Component that wraps the whole App like that :
<AppWrapper appCurrentLang="{appCurrentLang}"> {children} </AppWrapper>;
I don't think it causes the SSR to fail, because even if I add a <p> tag on the root layout, it still doesn't display in the body tag in the source.
Here is the first part of the content displayed after the html :
<script>(self.__next_f=self.__next_f||[]).push([0])</script><script>self.__next_f.push([1,"M1:{\"id\":\"./node_modules/next/dist/client/components/app-router.js\",\"name\":\"\",\"chunks\":[\"app-client-internals:app-client-internals\"],\"async\":false}\nM2:{\"id\":\"./app/providers.jsx\",\"name\":\"Providers\",\"chunks\":[\"app/layout:app/layout\"],\"async\":false}\nM3:{\"id\":\"./node_modules/next/dist/client/components/layout-router.js\",\"name\":\"\",\"chunks\":[\"app-client-internals:app-client-internals\"],\"async\":false}\nM4:{\"id\":\"./node_modules/next/dist/client/components/render-from-template-context.js\",\"name\":\"\",\"chunks\":[\"app-cl"])</script><script>self.__next_f.push([1,"ient-internals:app-client-internals\"],\"async\":false}\n"])</script><script>self.__next_f.push([1,"J0:[\"$\",\"#1\",null,{\"assetPrefix\":\"\",\"initialCanonicalUrl\":\"/posts/teslas-biggest-threat\",\"initialTree\":[\"\",{\"children\":[\"posts\",{\"children\":[[\"postURL\",\"teslas-biggest-threat\",\"oc\"],{\"children\":[\"\",{}]}]}]},null,null,true],\"initialHead\":[\"$\",\"title\",null,{\"children\":\"My TEST Next.js App\"}],\"children\":[[],[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/app/layout.css?ts=1676839989010\",\"precedence\":\"high\"}]],[\"$\",\"html\",null,{\"lang\":\"en\",\"data-test\":\"test\",\"children\":[\"$\",\"body\",null,{\"children\":[[\"$\",\"p\",null,{\"children\":\"okkk\"}],[\"$\",\"#2\",null,{\"langHeaders\":\"fr-FR\",\"children\":[\"$\",\"#3\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\"],\"hasLoading\":false,\"template\":[\"$\",\"#4\",null,{}],\"notFound\":[\"$\",\"div\",null,{\"style\":{\"fontFamily\":\"-apple-system, BlinkMacSystemFont, Roboto, \\\"Segoe UI\\\", \\\"Fira Sans\\\", Avenir, \\\"Helvetica Neue\\\", \\\"Lucida Grande\\\", sans-serif\",\"height\":\"100vh\",\"textAlign\":\"center\",\"display\":\"flex\",\"flexDirection\":\"column\",\"alignItems\":\"center\",\"justifyContent\":\"center\"},\"children\":[[\"$\",\"head\",null,{\"children\":[\"$\",\"title\",null,{\"children\":\"404: This page could not be found.\"}]}],[\"$\",\"div\",null,{\"children\":[[\"$\",\"style\",null,{\"dangerouslySetInnerHTML\":{\"__html\":\"\\n body { margin: 0; color: #000
Would anyone have an idea ?

Related

Google Chrome Developer tools -CSS file is shown empty

Thankyou for your precious time;
i'm a beginner in web development and as i was trying my hands on html and css
i found that the css styling isn't getting applied to the html and that the chrome
developer tools shows it to be empty (the file is present but the code that i've written in css file seems to be absent)
please help!
index.html file
<!DOCTYPE html>
<html>
<head>
<title>MY WEB PAGE </title>
<link type="text/css" href="style.css" rel="stylesheet" >
</head>
<body>
<h1>THIS IS MY WEB PAGE </h1>
<p>
css applied to this part of the code isnt being shown in the developers
tools
</p>
</body>
</html>
style.css file
body{
background-color:black;;
}
h1{
color:white;
font-style: italic;
font-family: sans-serif;
}
p{
font-family: serif;
color:yellow;
font-style: oblique;
}
You may be missing an angle bracket on your first line or it could be a copy-paste error. It should be: <!DOCTYPE html>
Also look in the Network tab in Chrome toolbar to make sure that it's able to find your style.css and it contains the right CSS.
Depending on how it's being hosted, you may have to hit Ctrl-F5 to tell the browser to download everything fresh instead of caching.
Clear the browser cache. It happens sometimes.
https://support.google.com/accounts/answer/32050?hl=en&co=GENIE.Platform%3DDesktop
you have to save css and html file in a same folder as your writed

How to detect if JavaScript is disabled using Meteor.com

When using Meteor.com anyone know how to detect if the browser's JavaScript is disabled...
AND show a message to the end user in the browser window, i.e "Turn on JavaScript."
I assume that you have a layout.html or at least a main.html that contains at least a <head>.
A trick is to place <noscript> in the <head> instead of the <body>.
Meteor does not render everything in JS. There are some stuff that get's rendered in an initial page, proof of which can be seen in View Source (CTRL+U).
I'm using the latest Meteor in Chrome, and head tags were not merged. Only this worked for me:
<head>
<noscript>Please enable JavaScript.</noscript>
</head>
<template name="index">
...
</template>
I just used this and it really worked like a charm
<head>
<noscript>
<style>
body {font-size: 32px;text-align: center;line-height: 100vh;}
body:after {content: "Please enable JavaScript";}
</style>
</noscript>
</head>

How to print an IFRAME containing multipe pages?

I would like to know if it is possible to print an HTML page containing an IFRAME that spans multiple pages. For some reason the browser keeps truncating the IFRAME after the first page. Here is a minimalistic example that demonstrates the issue outlined above. At first, here is a simple HTML page containing an IFRAME that should be optimized for printing:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TEST</title>
<style>
html, body {
height: 100%;
}
iframe {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<iframe src="iframe-content.html">
</iframe>
</body>
</html>
And here is the page I want to embed:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TEST-CONTENT</title>
</head>
<body>
<p>
Some very, very long text spawning multiple pages.
</p>
</body>
</html>
Using JavaScript is not an option. I'm looking for a solution that uses CSS only. Any help would be greatly appreciated.
You could specify in the meta of the parent doc because CSS does not have the ability specify the page to be printed, just how it looks.
<link rel=alternate media=print href="printme.html">
If you are trying to print the whole iframe inside the parent, you need to embed a print specific CSS doc. using this along with print preview will help you zero-in.
<link rel="stylesheet" href="print.css" media="print">
Only CSS solution - have to set iframe width to static size. For A4 Portrait the value of width is near of 670 pixels. For Landscape printing the value will be another.
iframe {
width: 670px;
}
Working in chromium browser.
My solution uses javascript to manipulate the DOM.
Also, your iframe needs an id attribute as shown below -- id='iframe1'
<body>
<iframe id='iframe1' src="iframe-content.html">
</iframe>
</body>
here is the javascript code:
<script>
pages =[] // initiate an empty list here
function printPage() {
// get the iframe and push its innerHTML into the list
var frame_content = document.getElementById('iframe1');
pages.push(frame_content.contentWindow.document.body.innerHTML);
if (pages && pages.length) {
// this if statement, just checks to ensure our list is not empty before running the code.
// Then we now set the parent window to be equal to pages
window.content.document.body.innerHTML = pages;
// then we print this new window that contains the iframe's innerHTML
window.print();
}
else {
// do nothing
}
}
With this solution, the print will not truncate your pages.
Also, in case you have multiple iframes, it generates only one print dialogue box.
Someone may wonder why i am pushing to a list. This caters for cases where you have more than one iframe hence you can easily push and concatenate all their innerHTML.
Remember that in your parent page HTML you will have the code below to call the printPage function.
<input type="submit" value="Print All"
onclick="javascript:printPage()"
/>

Can I see dynamically-generated CSS source in Chrome DevTools?

I loaded a Javascript widget that outputs HTML, CSS, and additional Javascript.
The source of the page (test.html) is just
<!DOCTYPE html>
<html lang="en">
<head>
<title>Widget Test</title>
</head>
<body>
<div id="widget"></div>
<script type="text/javascript" src="http://widgets.some.site/render?element_id=widget_id&customer_id=999999&gallery=22222&widget_config=769792669" async="async">
</script>
</body>
</html>
The script executes and generates a widget. I can see the HTML nodes in the Element tab of Chrome DevTools. When inspecting a particular element, the inspector says that its style is located at test.html:239, but when I click on the link, it shows me the source page again.
When I load the page in Firebug, clicking on the line number sends me to an internal version of the stylesheet maintained by Firefox. Is there a way Chrome DevTools does this as well? I like Firebug's output of dynamically generated CSS, since I can copy and paste very easily.
No, you won't be able to see the source, as for dynamically generated <style> tags it is not stored anywhere and is thrown away right after parsing, unlike for the external or inline stylesheets, which have the underlying source text (in the foo.css file or in the loaded document <style> tag, respectively).
You are navigated to the document, since you cannot see the stylesheet itself, and it is the DevTools' best effort in this case.

Eliminate flash of unstyled content

How do I stop the flash of unstyled content (FOUC) on a web page?
The problem with using a css style to initially hide some page elements, and then using javascript to change the style back to visible after page load, is that people who don't have javascript enabled will never get to see those elements. So it's a solution which does not degrade gracefully.
A better way therefore, is to use javascript to both initially hide as well as redisplay those elements after page load. Using jQuery, we might be tempted to do something like this:
$(document).ready(function() {
$('body').hide();
$(window).on('load', function() {
$('body').show();
});
});
However, if your page is very big with a lot of elements, then this code won't be applied soon enough (the document body won't be ready soon enough) and you might still see a FOUC. However, there is one element that we CAN hide as soon as script is encountered in the head, even before the document is ready: the HTML tag. So we could do something like this:
<html>
<head>
<!-- Other stuff like title and meta tags go here -->
<style type="text/css">
.hidden {display:none;}
</style>
<script type="text/javascript" src="/scripts/jquery.js"></script>
<script type="text/javascript">
$('html').addClass('hidden');
$(document).ready(function() { // EDIT: From Adam Zerner's comment below: Rather use load: $(window).on('load', function () {...});
$('html').show(); // EDIT: Can also use $('html').removeClass('hidden');
});
</script>
</head>
<body>
<!-- Body Content -->
</body>
</html>
Note that the jQuery addClass() method is called *outside* of the .ready() (or better, .on('load')) method.
This is the one that has worked for me and does not require javascript and it works great for pages with many elements and lots of css:
First, add a dedicated <STYLE> setting for the <HTML> tag with visibility 'hidden' and opacity as '0' at the top of your HTML, e.g, in the beginning of the <HEAD> element, for example, at the top of your HTML add:
<!doctype html>
<html>
<head>
<style>html{visibility: hidden;opacity:0;}</style>
Then, at the end of your last .css stylesheet file, set the visibility and opacity styles to 'visible' and '1', respectively:
html {
visibility: visible;
opacity: 1;
}
If you already have an existing style block for the 'html' tag, then move the entire 'html' style to the end of the last .css file and add the 'visibility' and 'opacity' tags as described above.
https://gist.github.com/electrotype/7960ddcc44bc4aea07a35603d1c41cb0
A CSS-only solution:
<html>
<head>
<style>
html {
display: none;
}
</style>
...
</head>
<body>
...
<link rel="stylesheet" href="app.css"> <!-- should set html { display: block; } -->
</body>
</html>
As the browser parses through the HTML file:
The first thing it will do is hide <html>.
The last thing it will do is load the styles, and then display all the content with styling applied.
The advantage to this over a solution that uses JavaScript is that it will work for users even if they have JavaScript disabled.
Note: you are allowed to put <link> inside of <body>. I do see it as a downside though, because it violates common practice. It would be nice if there was a defer attribute for <link> like there is for <script>, because that would allow us to put it in the <head> and still accomplish our goal.
A solution which doesn't depend on jQuery, which will work on all current browsers and do nothing on old browsers, include the following in your head tag:
<head>
...
<style type="text/css">
.fouc-fix { display:none; }
</style>
<script type="text/javascript">
try {
var elm=document.getElementsByTagName("html")[0];
var old=elm.class || "";
elm.class=old+" fouc-fix";
document.addEventListener("DOMContentLoaded",function(event) {
elm.class=old;
});
}
catch(thr) {
}
</script>
</head>
Thanks to #justastudent, I tried just setting elm.style.display="none"; and it appears to work as desired, at least in current Firefox Quantum. So here is a more compact solution, being, so far, the simplest thing I've found that works.
<script type="text/javascript">
var elm=document.getElementsByTagName("html")[0];
elm.style.display="none";
document.addEventListener("DOMContentLoaded",function(event) { elm.style.display="block"; });
</script>
An other quick fix which also works in Firefox Quantum is an empty <script> tag in the <head>. This however, penalizes your pagespeed insights and overall load time.
I had 100% success with it. I think it's also the main reason, why above solutions with other JS in the works.
<script type="text/javascript">
</script>
None of the CSS-only solutions presented here work with modern browsers (asynchronous loading of css and fonts). You have to use Javascript. What I've done to avoid FOUC is:
<html>
<body onload="document.body.style.visibility=`visible`;">
<script>document.body.style.visibility=`hidden`;</script>
With this approach the body of my web page is kept hidden until the full page and CSS files are loaded. Once everything is loaded, the onload event turns the body visible. So, the web browser remains empty until a point when everything pops up on the screen.
It is a simple solution but so far it is working.
This will not affect users who have disabled Javascript because the <script> tag is ignored.
No one has talked about CSS #import
That was the problem for me i was loading two extra style sheets directly in my css file with #import
Simple solution: Replace all #import links with <link />
Every answer on this page slows down the load and it only hides the underlying issue. If you're experiencing FOUC, find out WHY it's happening and fix that.
At the core, this is happening:
because your stylesheets are not being loaded correctly: they should be loaded via link tag in the HTML, not via JavaScript
because you placed script tags before link tags, which may force a "layout operation" and trick the browser into rendering before it even attempts to load the style.
For reference, here's an example of FOUC:
I came up with a way that requires no real code change whatsoever, woohoo! My issue was related to importing several css files AFTER some javascript files.
To resolve the issue I just moved my CSS links so that they would be above my javascript imports. This allowed all my CSS to be imported and ready to go ASAP, so that when the HTML appears on the screen, even if the JS isn't ready, the page will be properly formatted
Here is my code .. hope it solve your problem
set <body style="opacity:0;">
<script>
$(document).ready(function() {
$("body").css('opacity', 1);
});
</script>
A simple solution to avoid a flash of unstyled content without javascript:
<!DOCTYPE html>
<html>
<head>
<title>Bla bla</title>
<link href="..." rel="stylesheet" />
<link href="..." rel="stylesheet" />
</head>
<body style="opacity: 0">
<!-- All HTML content here -->
<script src="..."></script>
<script src="..."></script>
<style>
body {
opacity: 1 !important;
}
</style>
</body>
</html>
When the parser arrives at the body, it is faded out using "opacity: 0". When the parser finally arrives at the very bottom after everything else is parsed, the body is faded in again using an in-page style. The !important keyword there is important ;-), because it overrules the previous inline style of the body tag.
In this case, using "opacity: 0" to fade out is better than "display: none", because if you have layout operations done by javascript, they may not work when the affected element is not rendered.
That worked for me.
The best solution I found till now is like this:
Add all styles of your header to a <style/> tag in <head/>
at the top of style tag add .not-visible-first{visibility: hidden} + other header style
Add css via JS at the end of body
document.getElementsByTagName("head")[0].insertAdjacentHTML("beforeend","<link rel=\"stylesheet\" href=\"/css/main.min.css?v=1.2.4338\" />");
And remember to add .not-visible-first{visibility: visible} to the end of main.min.css
This option will create better user experience
You could try this with vanilla
function js_method(){
//todos
var elementDiv = document.getElementById("main");
elementDiv.style.display ="block";
}
<body onload="js_method()" id="main" style="display:none">
//todos
<h2>Hello</h2>
</body>

Resources