Nuxt JS 500 error when refreshing page loaded using JSON - json

I'm having a weird problem with my Nuxt JS website. I have created a system which uses Vue Resource and URL parameters to fetch JSON data to build a page, the idea is that it will eventually be controlled via a backend/API.
The problem I'm facing, is the pages work absolutely fine when in development mode running npm run dev, however, in production mode they break and cause a 500 internal server error when refreshing the page. But they don't break when loading the homepage and then navigating to page. (the homepage is a .vue file).
I'm not sure where to even begin with this problem. This is my page which essentially pulls data from a JSON file which is currently held locally, and each page is a JSON file right now. It works fine in development mode though.
<template>
<div class="page-json">
<pageBanner v-bind:bannerTitle="banner.bannerTitle" v-bind:bannerSubTitle="page.bannerSubTitle" v-bind:subTitleEnabled="page.bannerHasSubTitle" v-bind:bgClass="banner.bgClass" />
<section class="py-4 py-md-5">
<div class="container">
<div class="row py-4 py-md-5">
<div class="col-lg-4 col-md-4 col-sm-12">
<SearchComponent v-bind:orientationMargin="search.hasMargin" v-bind:searchPosition="search.orientation" v-bind:componentClass="search.searchClass" />
</div>
<div class="col-lg-8 col-md-8 col-sm-12 page-content">
<div v-html="page.content"></div>
<Alert v-bind:hasClass="errors.api.hasClass" v-bind:hasError="errors.api.hasError" v-bind:message="errors.api.message" v-bind:icon="errors.api.icon" />
</div>
</div>
</div>
</section>
</div>
</template>
<script>
import pageBanner from '~/components/PageBanner.vue'
import SearchComponent from '~/components/SearchComponent.vue'
import Alert from '~/components/Alert.vue'
export default {
components: {
pageBanner,
SearchComponent,
Alert
},
data () {
return {
slug: this.$route.params.slug,
page: [],
banner: {
bannerTitle: 'Page',
bgClass: 'jumbotron-houses jumbotron-page'
},
search: {
searchClass: 'mb-4 mb-md-0',
hasMargin: 'mb-3',
orientation: 'd-block'
},
errors: {
api: {
hasError: false,
hasClass: 'alert-danger mt-3 mt-md-0',
message: 'We was unable to fetch you property data.',
icon: 'error'
}
}
}
},
created() {
this.$http.get('/pages/' + this.slug + '.json').then(response => {
this.page = response.body
}, response => {
// this.$router.push('/404')
});
},
head () {
return {
title: this.page.pageTitle + ' | Site',
meta: [
{ hid: 'description', name: 'description', content: this.page.pageDescription + ' - Site' }
]
}
}
}
</script>
Not entirely sure why it causes an internal server error when refreshing the page or visiting the page directly.
Can anyone help?
UPDATE:
Could this be because of a missing .htaccess file / rewrites? In production mode there is no .htaccess file for the dist folder?
This is my Apache error log:
[Tue Sep 25 11:51:39 2018] [error] [client ::1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://property-boilerplate.ryan/testimonials

Related

New google sign in disappears after refreshing page

I followed the guidelines that provided by google to integrate new google sign in. I created HTML using Code generator that provided by Google.
Here I have attached the complete code
<svelte:head>
<title>Home</title>
<meta name="description" content="Svelte demo app" />
</svelte:head>
<section>
<div class="h-screen">
<div
id="g_id_onload"
data-client_id="534101779287-bm07dc8v4ln4kulqbql61nsglcku74vg.apps.googleusercontent.com"
data-context="use"
data-ux_mode="redirect"
data-login_uri="http://localhost:5173/auth/callback"
/>
<div class="bg-red-300 h-80">
<div
class="g_id_signin"
data-type="standard"
data-shape="rectangular"
data-theme="outline"
data-text="signin_with"
data-size="medium"
data-logo_alignment="left"
data-width="180"
/>
</div>
</div>
</section>
It works fine for the first time render of the page.
When we are refreshing the page using Command+R or by clicking reload icon from the browser, Sign in button disappears.
A hard reload is server-side rendered when using SvelteKit. The code is probably incompatible with that or the execution order is wrong.
Check the console for errors and move code that has to run on the client to onMount. You can also turn off server-side rendering for specific pages using the ssr page option as a last resort.
For now I created component using Javascript, Here I have added the answer.
I declared google as global variable in app.d.ts
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
declare global {
const google: any;
namespace App {
}
}
export {};
I created a svelte file to create a svelte component for sign in button
let canvas: any; //Created a variable to optain a reference to rendered div element
<div class="g_id_signin"
bind:this={canvas}/>
In onMount
onMount(async () => {
google.accounts.id.initialize({
client_id: "534101779287-bm07dc8v4ln4kulqbql61nsglcku74vg.apps.googleusercontent.com",
ux_mode: "redirect",
context: "use",
login_uri: "http://localhost:5173/auth/callback"
});
google.accounts.id.renderButton(canvas, {
width: '220',
theme: 'outline',
size: 'large',
type: 'standard',
text: 'signin_with',
shape: 'rectangular',
logo_alignment: 'left',
});
});
This code will work in initial render, Hard reload (Command+shift+R) and Reload (Command+R)

Better way to return gutenberg block data from Wordpress post as json - Headless Wordpress Vue

I am currently testing out Headless Wordpress with Vue. Right now I am trying to accomplish to return page content with all gutenberg blocks(Advanced Custom Fields etc).
For this I am using wp-json/wp/v2/pages api to return all pages.
Problem was that this api didn't return content with gutenberg blocks so I decided to use plugin called "REST API blocks". With this I managed to get gutenberg blocks data inside page content.
Is there a way to return gutenberg data inside content without plugin? I wouldn't want to rely on plugin especially when it's outdated.
My code
<template>
<div>
<div v-for="page in pages" v-bind:key="page.id">
<h2>{{ page.title.rendered }}</h2>
//loop trough all gutenberg blocks inside page
<div v-for="block in page.blocks">
//page content as html.
<div v-html="block.attrs.content"></div>
//acf block data. Get field named title
{{ block.attrs.data?.title }}
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
pages: [],
};
},
methods: {
async getData() {
try {
let response = await fetch("http://localhost:8000/wp-json/wp/v2/pages");
this.pages = await response.json();
console.log(response);
} catch (error) {
console.log(error);
}
},
},
created() {
this.getData();
},
};
</script>
My json looks like this https://justpaste.it/b43wy
This may not be the most correct way to implement Headless Wordpress with Vue but I am just doing it for learning puropouses.
Any information about Headless Wordpress is very welcome and I am up for discussion and new knowledge.

Polymer/Lit-Element - Braintree gateway integration - Web Component problem

I am trying to integrate Braintree payment gateway to Vaadin 14 which is using Polymer for its frontend.
Basically we have a custom Vaadin front-end view to load script https://js.braintreegateway.com/web/dropin/1.9.4/js/dropin.min.js :
And we call its method dropin.create as below:
import{PolymerElement}from'#polymer/polymer/polymer-element.js';
import'#polymer/iron-icon/iron-icon.js';
import{html}from'#polymer/polymer/lib/utils/html-tag.js';
import '#webcomponents/webcomponentsjs/webcomponents-loader.js';
import'#polymer/polymer/polymer-legacy.js';
import'#polymer/iron-flex-layout/iron-flex-layout.js';
import{mixinBehaviors}from'#polymer/polymer/lib/legacy/class.js';
import{Polymer}from'#polymer/polymer/lib/legacy/polymer-fn.js';
import{setTouchAction}from'#polymer/polymer/lib/utils/gestures.js';
import{afterNextRender}from'#polymer/polymer/lib/utils/render-status.js';
import'#vaadin/vaadin-text-field/vaadin-text-field.js';
import { sharedStyles } from './drop-in.js';
let dropin = require('braintree-web-drop-in');
class BrainTreeVaadin extends PolymerElement {
<vaadin-vertical-layout class="main-div-layout-boder padding5">
<form id="paymentForm" method="post" action="/checkout" class="main-screen-vert-layout-row">
<div id="containPayment" class="main-screen-vert-layout-row">
<div id="btDropin" class="main-screen-vert-layout-row"></div>
</div>
<vaadin-text-field id="nonce" value={{valueNonce}} hidden></vaadin-text-field>
<vaadin-button id="butPayment" theme="theme-button-02" class="button-row">Payment</vaadin-button>
</form>
</vaadin-vertical-layout>
createFormPayment(){
let form = this.$.paymentForm;
let butPayment = this.$.butPayment;
let btDropin = this.$.btDropin;
let textNonce = this.$.nonce;
dropin.create({
authorization: this.clientToken,
container: btDropin,
card: {
cardholderName: {
required: true
}
},
paypal: {
flow: 'vault',
currency: 'USD'
},
paypalCredit: {
flow: 'vault',
currency: 'USD'
}
}
}
}
However we get error as below image:
Reason that internally, the main script dropin.min.js includes other script https://www.paypalobjects.com/api/checkout.min.js and called other methods from this new JS.
Accessing methods in checkout.min.js got error because checkout.min.js can’t get id of html elements (here is buttons) using javascript reference style "#...".
Braintree uses JS style #element_id to pass a html div element as argument to method: braintree.dropin.create(..., container: '#bt-dropin').
Below is Braintree example code (take note on method "braintree.dropin.create", it takes '#bt-dropin' as input):
<div class="bt-drop-in-wrapper">
<div id="bt-dropin"></div>
</div>
<div th:include="fragments/homefooter :: footer"></div>
<script src="https://js.braintreegateway.com/web/dropin/1.9.4/js/dropin.min.js"></script>
<script th:inline="javascript">
/*<![CDATA[*/
var form = document.querySelector('#payment-form');
var client_token = [[${clientToken}]];
braintree.dropin.create({
authorization: client_token,
container: '#bt-dropin',
paypal: {
flow: 'vault'
}
}, function (createErr, instance) {
form.addEventListener('submit', function (event) {
event.preventDefault();
$('#errorDiv').hide();
$('#serverSideErrorDiv').hide();
instance.requestPaymentMethod(function (err, payload) {
if (err) {
console.log('Error', err);
showError(err);
return;
}
// Add the nonce to the form and submit
document.querySelector('#nonce').value = payload.nonce;
form.submit();
});
});
});
And problem that Vaadin form (view) doesn’t understand javascript style: "#bt-dropin" to reference to a div element.
How to make Vaadin view understand JS style: "#element_id" ?
Update:
this is polymer problem, not Vaadin flow problem.
Update 2:
this is braintree problem, not polymer problem :)).
This is a issue of braintree due to lacking of supporting web components.
Below is workaround solution.
Braintree Git Issue
Workaround (remove space .io on URL):
https://codepen .io/braintree/pen/VrYXYW

How to load static images on ExpressJs

so I have this node/express api that serve MySQL database, has json output like this:
[
{
id: 1,
name: "Laptop Lenovo",
serial: "123-456",
tag: "IT-CL-22052018-001",
image: "/public/images/lenovo.jpg"
},
{
id: 2,
name: "Desktop Dell",
serial: "456-123",
tag: "IT.CD.19052018-002",
image: "public/images/dell.jpg"
},
{
id: 3,
name: "Laptop Dell",
serial: "909090",
tag: "IT.CL.01052018.002",
image: "http://localhost:8000/images/ldell.jpg"
}
]
I tried this express functions each one of it:
app.use(express.static('public'))
app.use('/static', express.static('public'))
var dir = path.join(__dirname, 'public');
app.use(express.static(dir));
And the only way my React app can display the image is by using the full url on the database, like #3.
How I can display the image without using the full url?
Edit: Add Render Function
render() {
return this.state.inventory.map(itemList => {
let item = itemList;
return (
// <div className="tile">
<div className="card margin-all">
<div className="card-content">
<div className="media">
<div>
<figure className="image is-96x96">
<img src={item.image} />
</figure>
</div>
</div>
<div>
<h4>Nama: {item.name} </h4>
<h4>Nomor Seri: {item.serial} </h4>
<h4>ID Tag: {item.tag} </h4>
</div>
</div>
</div>
// </div>
);
})
}
}
Pict
For express middleware app.use(express.static('public')) is fine, problem is you need to use correct path. In you json data, image is public/images/dell.jpg which should be /public/images/dell.jpg but you can use
<img src={ '/' + item.image} />
as well, hope this will help
The route for static assets is /static/ so I assume you must call the images like this (e.g. #2)
app.use('/static', express.static('public'))
But you're requesting it via /public/
{
id: 2,
name: "Desktop Dell",
serial: "456-123",
tag: "IT.CD.19052018-002",
image: "static/images/dell.jpg"
}
So if you want the path from your origin JSON get working change it to
app.use('/public', express.static(__dirname + '/public'));
From express.js documentation:
http://expressjs.com/en/starter/static-files.html
However, the path that you provide to the express.static function is relative to the directory from where you launch your node process. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve
So by adding a proxy to React app, I don't need to provide full url to the link.
If the express port is 8000, then put this line in the package.json of the React App, not the Node app.
"proxy": "http://localhost:8000"
Now I can use images/name.jpg and the image will be displayed.

Multiple UI-Views not working as expected

I am trying to use multiple UI-views in my AngularJS app and it is not working as expected. The app is a dashboard.
My directory structure is as follows:
index.html
app/
app.js
dashboard/
dashboard.html
dashboard.js
partials/
business.html
items.html
orders.html
sales.html
login/
login.html
login.js
The problem that I am having is that my index.html file has the following line of code:
<div ui-view></div>
The above line enables my application to show the login.html page and dashboard.html page. Now I want to be able to have partial views in my dashboard.html page and so I have also put the same line of code
<div ui-view></div>
in order to be able to embed partial views in my dashboard page. Instead of embedding though, the page instead just redirects. So for example if I am in my dashboard.html and click on a navigation item such as 'business', I am redirected to partials/business.html instead of the content being embedded.
1) Can I have multiple ui-views embedded within each other?
2) Am I correctly embedding the partial views?
I have scoured the internet but cannot find a solution. Thanks in advance for the help!
You can definitely have multiple embedded views.
Check out these AngularJS UI-Router tutorials: Nested Views and Multiple Named Views.
Let me know if you still have issues after looking them over.
You can define a ui-view inside another ui-view. I have implemented it in the following manner and its pretty straight forward.
Inside index.html I have code:
<div ui-view=""></div>
Then inside user.html I have code
<div ui-view=""></div>
And I have defined a config for displaying my views as
.config(function($urlRouterProvider, $stateProvider) {
var users = {
//Name must be in format Parent.Child
name: 'users',
url: '/user',
templateUrl: 'users/user.html',
controller: 'usersHandler',
data: {
pageTitle: 'Welcome to Users'
},
},
createUsers = {
name: 'users.createUsers',
url: '/createUser',
templateUrl: 'users/createUser.html',
data: {
pageTitle: 'Create Users'
}
},
listUsers = {
name: 'users.listUsers',
url: '/listUsers',
templateUrl: 'users/userLists.html',
data: {
pageTitle: 'Users listing'
}
},
getUserDealer = {
name: 'users.getUserDealer',
url: '/getUserDealer',
templateUrl: 'users/getUserDealer.html',
data: {
pageTitle: 'Users dealer listing'
}
},
editUser = {
name: 'users.editUser',
url: '/editUser',
templateUrl: 'users/editUser.html',
data: {
pageTitle: 'Edit User'
}
};
//Similarly define all the combination you want to separate
//add routes to stateProvider
$stateProvider
.state('users', users)
.state('users.createUsers', createUsers)
.state('users.listUsers', listUsers)
.state('users.getUserDealer', getUserDealer)
.state('users.editUser', editUser);
$urlRouterProvider.otherwise('/user/listUsers');
});
Whats happening is this that user.html is my parent file which is loaded inside index.html and editUser.html, getUserDealer.html and userLists.html etc are its children which I load within user.html using ui-view.
And I provide the links for nested pages as:
<ul class="nav navbar-nav">
<li>NEW USER</li>
<li>GET USER</li>
</ul>
This can be extended to additional parents and their children as per the need.
Hope it helps!!