Skip to content

Commit

Permalink
Render posts on the server for SEO purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
kutaloweb committed Feb 11, 2019
1 parent 412f48e commit 81f6c54
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 32 deletions.
7 changes: 5 additions & 2 deletions app/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ public function getTotallyStrippedBodyAttribute()
$string = $this->attributes['body'];
$doubleSpace = strip_tags(str_replace('<', ' <', $string));
$singleSpace = str_replace(' ', ' ', $doubleSpace);

return trim($singleSpace);
$string = trim($singleSpace);
if (mb_strlen($string, "utf-8") > 150) {
$string = mb_substr($string, 0, 150, "utf-8") . '...';
}
return $string;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ public function getTotallyStrippedBodyAttribute()
$string = $this->attributes['body'];
$doubleSpace = strip_tags(str_replace('<', ' <', $string));
$singleSpace = str_replace(' ', ' ', $doubleSpace);

return trim($singleSpace);
$string = trim($singleSpace);
if (mb_strlen($string, "utf-8") > 150) {
$string = mb_substr($string, 0, 150, "utf-8") . '...';
}
return $string;
}

/**
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"fideloper/proxy": "^4.0",
"guzzlehttp/guzzle": "^6.3",
"intervention/image": "^2.4",
"jaybizzle/laravel-crawler-detect": "1.*",
"laravel/framework": "5.6.*",
"laravel/tinker": "^1.0",
"mews/purifier": "^2.0",
Expand Down
4 changes: 3 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
*/
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
Nutsweb\LaravelPrerender\LaravelPrerenderServiceProvider::class,
Jaybizzle\LaravelCrawlerDetect\LaravelCrawlerDetectServiceProvider::class,

/*
* Application Service Providers...
Expand Down Expand Up @@ -208,7 +209,8 @@
'Storage' => Illuminate\Support\Facades\Storage::class,
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class
'View' => Illuminate\Support\Facades\View::class,
'Crawler' => Jaybizzle\LaravelCrawlerDetect\Facades\LaravelCrawlerDetect::class,
],

];
2 changes: 1 addition & 1 deletion config/prerender.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| prerender on your local machine.
|
*/
'enable' => env('PRERENDER_ENABLE', true),
'enable' => env('PRERENDER_ENABLE', false),
/*
|--------------------------------------------------------------------------
| Prerender URL
Expand Down
20 changes: 6 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Spala

Content Management System based on [Laravel 5.6](https://laravel.com), [Vue 2.5](https://vuejs.org), [Bootstrap 4](https://getbootstrap.com/) and [Monster Admin Template](https://wrappixel.com/demos/admin-templates/monster-admin/Documentation/document.html).
Content Management System based on [Laravel 5.6](https://laravel.com), [Vue 2.5](https://vuejs.org), [Bootstrap 4](https://getbootstrap.com/) and [Monster Admin Template](https://wrappixel.com/demos/admin-templates/monster-admin/Documentation/document.html)

## Getting Started

These instructions will get you a copy of the project up and running on your local Linux or Mac OS X machine.
These instructions will get you a copy of the project up and running on your local Linux or Mac OS X machine

### Installing

Expand Down Expand Up @@ -58,7 +58,7 @@ Execute the NPM script
npm run dev
```

Change the group ownership of the storage and cache directories and grant them all permissions (for Mac type `_www` instead of `www-data`).
Change the group ownership of the storage and cache directories and grant them all permissions (for Mac type `_www` instead of `www-data`)

```
sudo chgrp -R www-data storage bootstrap/cache
Expand All @@ -71,21 +71,13 @@ Install the application (create default roles, permissions, etc.)
php artisan install
```

Go to the page `/register`. The first registered user will get the admin role.
Go to the page `/register`. The first registered user will get the admin role

If you need own prerender server for SEO purposes

```
git clone https://github.com/prerender/prerender.git
cd prerender
npm install
npm install -g forever
forever start server.js
```
By default server-side rendering is used for the SEO purposes but if you need your own prerender server you can use [prerender.io](https://prerender.io/)

## Contributing

As an open project, I welcome contributions from everybody. Please, feel free to fork the repository and submit pull requests.
As an open project, I welcome contributions from everybody. Please, feel free to fork the repository and submit pull requests

## License

Expand Down
9 changes: 3 additions & 6 deletions resources/assets/js/views/page/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@
return {
title: `${this.documentTitle}`,
meta: [
{name: 'description', content: this.page ? this.limitWords(this.page.totally_stripped_body) : ''},
{name: 'description', content: this.page ? this.page.totally_stripped_body : ''},
{name: 'twitter:card', content: 'summary_large_image'},
{name: 'twitter:title', content: this.page ? this.page.title : ''},
{name: 'twitter:description', content: this.page ? this.limitWords(this.page.totally_stripped_body) : ''},
{name: 'twitter:description', content: this.page ? this.page.totally_stripped_body : ''},
{name: 'twitter:image', content: `${this.getConfig('app_url')}/uploads/images/cover-default.png`},
{property: 'og:type', content: 'website'},
{property: 'og:site_name', content: this.getConfig('company_name')},
{property: 'og:url', content: this.page ? `${this.getConfig('app_url')}/${this.page.slug}` : ''},
{property: 'og:title', content: this.page ? this.page.title : ''},
{property: 'og:description', content: this.page ? this.limitWords(this.page.totally_stripped_body) : ''},
{property: 'og:description', content: this.page ? this.page.totally_stripped_body : ''},
{property: 'og:image', content: `${this.getConfig('app_url')}/uploads/images/cover-default.png`}
]
}
Expand Down Expand Up @@ -96,9 +96,6 @@
getConfig(name) {
return helper.getConfig(name);
},
limitWords(str) {
return helper.limitWords(str, 35);
},
searchCategory(categoryId) {
helper.showSpinner();
this.$store.dispatch('setSearchCategory', categoryId);
Expand Down
9 changes: 3 additions & 6 deletions resources/assets/js/views/post/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@
return {
title: `${this.documentTitle}`,
meta: [
{name: 'description', content: this.post ? this.limitWords(this.post.totally_stripped_body) : ''},
{name: 'description', content: this.post ? this.post.totally_stripped_body : ''},
{name: 'twitter:card', content: 'summary_large_image'},
{name: 'twitter:title', content: this.post ? this.post.title : ''},
{name: 'twitter:description', content: this.post ? this.limitWords(this.post.totally_stripped_body) : ''},
{name: 'twitter:description', content: this.post ? this.post.totally_stripped_body : ''},
{name: 'twitter:image', content: this.post ? `${this.getConfig('app_url')}/${this.post.cover}` : ''},
{property: 'og:type', content: 'website'},
{property: 'og:site_name', content: this.getConfig('company_name')},
{property: 'og:url', content: this.post ? `${this.getConfig('app_url')}/${this.categorySlug}/${this.post.slug}` : ''},
{property: 'og:title', content: this.post ? this.post.title : ''},
{property: 'og:description', content: this.post ? this.limitWords(this.post.totally_stripped_body) : ''},
{property: 'og:description', content: this.post ? this.post.totally_stripped_body : ''},
{property: 'og:image', content: this.post ? `${this.getConfig('app_url')}/${this.post.cover}` : ''}
]
}
Expand Down Expand Up @@ -105,9 +105,6 @@
getConfig(name) {
return helper.getConfig(name);
},
limitWords(str) {
return helper.limitWords(str, 35);
},
searchCategory(categoryId) {
helper.showSpinner();
this.$store.dispatch('setSearchCategory', categoryId);
Expand Down
29 changes: 29 additions & 0 deletions resources/views/seo.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<title>{{ !empty($article->title) ? $article->title . ' | ' . config('config.company_name') : config('config.company_name')}}</title>
<meta name="description" content="{{ $article->totally_stripped_body or config('config.company_description') }}">

<meta property="og:type" content="article"/>
<meta property="og:site_name" content="{{ config('config.company_name')}}"/>
<meta property="og:url" content="{{ Request::url() }}"/>
<meta property="og:title" content="{{ $article->title or config('config.company_name') }}"/>
<meta property="og:description" content="{{ $article->totally_stripped_body or config('config.company_description') }}"/>
<meta property="og:image" content="{{ !empty($article->cover) ? config('config.app_url') . '/' . $article->cover : config('config.app_url') . '/' . config('config.default_cover') }}"/>

<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:description" content="{{ $article->totally_stripped_body or config('config.company_description') }}"/>
<meta name="twitter:title" content="{{ $article->title or config('config.company_name') }}"/>
<meta name="twitter:image" content="{{ !empty($article->cover) ? config('config.app_url') . '/' . $article->cover : config('config.app_url') . '/' . config('config.default_cover') }}"/>
</head>
<body>

<div>
<h1>{{ $article->title or config('config.company_name') }}</h1>
<div>{!! $article->stripped_body or config('config.company_description') !!}</div>
</div>
</body>
</html>
6 changes: 6 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@
})->name('assets.lang');

Route::get('/{vue?}', function () {
if(Crawler::isCrawler()) {
$category = App\Category::where('slug', Request::segment(1))->first();
if (empty($category)) return view('seo')->with('article', null);
$article = App\Post::filterByCategoryAndSlug($category, Request::segment(2))->first();
return view('seo')->with('article', $article);
}
return view('home');
})->where('vue', '[\/\w\.-]*')->name('home');

0 comments on commit 81f6c54

Please sign in to comment.